我創建了一個列表框asp.net razor
。但我想能夠多選列表框。我怎麼做?如何多選列表框asp.net
我想用這樣的:
[HttpPost]
public ActionResult KenmerkSelectie(List<string> VarFromKenmerk1)
{
}
下面的列表框的職位只有一個變量:
@using (Html.BeginForm("KenmerkSelectie", "KenmerkSelectie", FormMethod.Post, new { id = "kenmerk1" }))
{
@Html.ListBox("VarFromKenmerk1", (SelectList)ViewBag.var)
<input type="submit" value="submit"/>
}
編輯: 我想下面的代碼,但我得到一個錯誤:
@using (Html.BeginForm("KenmerkSelectie", "KenmerkSelectie", FormMethod.Post, new { id = "kenmerk1" }))
{
@Html.ListBoxFor("VarFromKenmerkTEST",(MultiSelectList)ViewBag.var), new {@style = "width: 252px; height: 300px;"})
<input type="submit" value="submit"/>
}
public ActionResult index()
{
ViewBag.VarFromKenmerk1 = new List<SelectListItem>();
ViewBag.VarFromKenmerk1.Add(new SelectListItem
{
Value = "House",
Text = "House"
});
ViewBag.VarFromKenmerkTEST = new MultiSelectList(ViewBag.VarFromKenmerk1);
return View();
}
[HttpPost]
public ActionResult KenmerkSelectie(List<string> VarFromKenmerkTEST){
foreach(var ken in VarFromKenmerkTEST){
//selected items
}
}
Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.SelectExtensions.ListBoxFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
Source Error: @Html.ListBoxFor("VarFromKenmerkTEST",(MultiSelectList)ViewBag.var), new {@style = "width: 252px; height: 300px;"})
我不知道KenmerkSelectie中的參數是否好。
你是什麼意思_「只發布1個變量」_?它會回發一組綁定到你的參數的值(如果你選擇了3個選項,那麼'VarFromKenmerk1'將包含3個值) –
[編輯視圖中的超級簡單實現多選列表框的可能的重複] (http://stackoverflow.com/questions/18363158/super-simple-implementation-of-multiselect-list-box-in-edit-view) –