管理帐户mvc app c#[关闭]

问题描述 投票:0回答:1

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

ePic of my accounts page

调节器

//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>
c# asp.net asp.net-mvc-4 model-view-controller
1个回答
2
投票

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

调节器

public ActionResult ViewAccounts(int id)
{

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

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

© www.soinside.com 2019 - 2024. All rights reserved.