簡體   English   中英

下拉列表MVC4

[英]Drop Down List MVC4

我正在構建一個部分視圖,其中顯示了用戶可以選擇的帳戶列表。 它顯示在布局頁面上,因為它們可以在會話中的任何時間在帳戶之間切換。

在我的控制器中,我返回了一個帳戶列表,並為部分視圖構建了一個模型:

public ActionResult AccountPortfolios()
        {
            var personId = int.Parse(User.Identity.Name);
            var ports = new AccountService().GetPortfoliosByPersonId(personId);

            var result = new List<AccountPortfolioListModel>();

            foreach (var port in ports)
            {
                result.Add(new AccountPortfolioListModel
                    {
                        Description = port.Description,
                        Id = port.Id
                    });
            }

            return View(result);

        }

然后我將模型( List<AccountPortfolioListModel>()傳遞給我的View。

在我看來,我將模型聲明為:

@model List<BasicFinanceUI.Models.AccountPortfolioListModel>

但后來我被卡住了。 我想在下拉列表中顯示列表,使用按鈕選擇所選列表,然后調用Post方法。

另外,我使用正確的東西嗎? 部分視圖顯示我的布局頁面上可見的內容下拉列表?

我們喜歡使用靜態調用來填充下拉列表

@Html.DropDownListFor(x => x.Account, PathToController.AccountList())

然后在你的控制器上

public static List<SelectListItem> AccountList(){
    List<SelectListItem> ls = new List<SelectListItem>();
    //populate your list
    return ls;
}

我不知道你只需要一個部分視圖來下拉。 我如何做到這一點是將輸入變量放在帳戶的索引操作上

public ActionResult Index(string account)

然后在你的按鈕點擊事件

window.location = '@Url.Action("Index", "Controller", new { account = "----" })'.replace("----", $('#Account').val());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM