【问题标题】:manage account mvc app c# [closed]管理帐户 mvc app c# [关闭]
【发布时间】:2018-06-02 02:34:05
【问题描述】:

我正在尝试让我的“管理”链接重定向到相关帐户。 我有一个 BankAccsController 可以很好地查看登录用户的详细信息并查看数据库中持有的两个帐户,但我想要做的是能够单击帐户右侧的相应按钮并让它带我查看该特定帐户的详细信息。我知道我必须创建一个新方法来替换当前的两个“ViewCurrent 和 ViewSavings”并使用参数,但我不确定如何去做,也找不到正确的词来将其输入 Google。

控制器

//View all accounts of logged in user
    [Authorize]
    public ActionResult Index()
    {
        var userId = User.Identity.GetUserId();
        var bankAccs = db.BankAccs.Where(a => a.AccUserId == userId).ToList();
        return View(bankAccs.ToList());
    }

    ////View current account  of logged in user
    public ActionResult ViewCurrent()
    {

        var userId = User.Identity.GetUserId();
        ViewModels.CurrentAccVm bankVm = new ViewModels.CurrentAccVm();
        bankVm.BankAccList = db.BankAccs.Where(a => a.AccUserId == userId && a.AccTypeId == 1).ToList();
        bankVm.UserName = User.Identity.GetUserName();
        return View(bankVm);
    }

    ////View Savings account  of logged in user
    public ActionResult ViewSavings()
    {
        var userId = User.Identity.GetUserId();
        ViewModels.CurrentAccVm bankVm = new ViewModels.CurrentAccVm();
        bankVm.BankAccList = db.BankAccs.Where(a => a.AccUserId == userId && a.AccTypeId == 2).ToList();
        bankVm.UserName = User.Identity.GetUserName();
        return View(bankVm);
    }

查看

     @model IEnumerable<Atm11.Models.BankAcc>

    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Balance)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.AccType.AccountType)
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Balance)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AccType.AccountType)
            </td>
            <td>
                @Html.ActionLink("Manage", "ViewAccounts", new { id = item.Id })

            </td>
        </tr>
    }

    </table>

【问题讨论】:

  • 我不清楚你在问什么。您有一个指向名为ViewAccounts 的操作的链接。您是否创建了该操作?是什么阻止您创建它?
  • 我还没有创建一个,不。那是我不确定的部分。我不知道如何编写正确传递参数的方法和正确的语法。干杯
  • 老实说,听起来您正在寻找一些关于 ASP.NET MVC 的介绍性教程。创建一个动作方法通常在那里。提示:您的新方法将被称为“ViewAccounts”,将有一个名为“id”的参数,并返回一个“ActionResult”。
  • 好的,非常感谢

标签: c# asp.net asp.net-mvc-4 model-view-controller


【解决方案1】:

你离得太近了!将以下方法添加到您的控制器。您的视图构造正确,因此不需要对该文件进行任何更改。

控制器

public ActionResult ViewAccounts(int id)
{

    var myAccount = DB.Accounts.GetByID(id);
    return View(myAccount);
}

接下来,您需要创建一个 ViewAccounts 视图。

【讨论】:

  • 好的,非常感谢。我试试看。
  • @robbiedarza 请将此答案标记为已接受。这是您所问问题的答案。为了这个问题的未来扩展,请创建一个新问题。
  • 非常感谢您现在整理。
猜你喜欢
  • 1970-01-01
  • 2015-03-28
  • 2016-11-27
  • 2013-11-07
  • 2013-07-07
  • 1970-01-01
  • 2012-09-23
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多