管理帐户 mvc 应用程序 c#

manage account mvc app c#

我正在尝试让我的“管理”link 重定向到相关帐户。 我有一个 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>

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

控制器

public ActionResult ViewAccounts(int id)
{

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

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