我是ASP.NET MVC4的新手。我有任何问题。
我使用DropDownListFor()创建DropDownList
示例:我在视图中使用此代码
@Html.DropDownListFor(p => p.createAccount.AccountId, Model.AccountNameListData)
模型中的AccountNameListData
public IEnumerable<SelectListItem> AccountNameListData { get; set; }
并且这是我的示例DropDownList(在此图片之后)
问题
如果我在模型中使用此代码列出数据
public IEnumerable<SelectListItem> AccountNumberListData { get; set; }
示例:此值将数据保存为{001,002,003,004,005,006}
当我点击DropDownList时,我想看到
001 - Pretty Casg
002 - Exchange
003 - Pretty Cash
004 - I/O Ex
005 - Petty Cash
006 - Hello World
Ex中定义模型中的public IEnumerable<SelectListItem> keepValue {get; set;}
在视图Model.KeepValue = Model.AccountNumberListData - Model.AccountNameListData
@Html.DropDownListFor(p => p.createAccount.AccountId,Model.KeepValue )
但这不起作用。
请帮帮我T ^ T
答案 0 :(得分:0)
[AcceptVerbs(Http.Get)]
public ActionResult GetValueExample() {
ViewData["MyItems"] = GetSelectList();
return View();
}
[AcceptVerbs(Http.Get)]
public ActionResult GetValueExample(string MyList) {
//MyList contains the selected value
SelectList list = GetSelectList(); //list will contain the original list of items
//...
}
private SelectList GetSelectList() {
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("Item 1", "1");
list.Add("Item 2", "2");
list.Add("Item 3", "3");
return new SelectList(list, "value", "key");
}
//...
<%= Html.DropDownList("MyList", ViewData["MyItems"] as SelectList) %>