Asp.net-Mvc
Bootstrap Multiselect 獲取 HttpPost 上的選定值
我正在使用這個Bootstrap Multiselect,我的問題是我無法在 ASP.Net MVC 上的 HttpPost 上獲取選定的值。
遇到的問題:
- 點擊保存後,模型上只有第一個選擇的值。(已解決)
- 點擊保存後,只有第一個選定的值出現在下拉列表中。
範例.chtml:
@model SampleProject.Models.SampleViewModel @using (Html.BeginForm()) { @Html.DropDownListFor(model => model.Selected, new SelectList(Model.List, "value", "text"), new { @class = "multiselect form-control", multiple = "multiple" }) <input type="submit" value="Save" /> }模型:
public class SampleViewModel { public string[] Selected { get; set; } public SelectList List { get; set; } }控制器:
public class DashboardController : Controller { public ActionResult Sample() { SampleViewModel model = new SampleViewModel(); model.List = new SelectList(new List<string>() { "1", "2", "3" }); return View(model); } [HttpPost] public ActionResult Sample(SampleViewModel model) { model.List = new SelectList(new List<string>() { "1", "2", "3" }); return View(model); } }選擇:
我無法在 HttpPost 上正確獲取所選值
程式碼背後/HttpPost:(錯誤)
HttpPost 之後:(正確)
A
<select multiple="multiple">回發一組值(不是單個值)。例如,您的財產Selected必須是IEnumerablepublic string[] Selected { get; set; }旁注:由於 text 和 value 屬性相同,您可以通過製作
List屬性來簡化程式碼SelectListpublic SelectList List { get; set; }然後在控制器中
model.List = new SelectList(new List<string>() { "1", "2", "3" })(雖然不清楚你為什麼不使用
int)
在任何地方都沒有看到這個答案,因此要解決在回發後只選擇一項的問題,您需要使用:
@Html.ListBoxFor(.. instead of @Html.DropDownListFor


