问题描述
我使用这个以及我的问题是,我不能得到所选择的值上HttpPost上的ASP.Net MVC。
I'm using this Bootstrap Multiselect and my problem is that I cant get the selected values on HttpPost on ASP.Net MVC.
问题:
-
点击保存后,只有第一个选择的值是present上
该模型。 (解决)
点击保存后,只有第一个选择的值是present上
DropDownList的。
after clicking save, Only the first selected value is the present onthe dropdownlist.
Sample.chtml:
Sample.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选定值
code的背后/ HttpPost:(错)
Code Behind/HttpPost: (Wrong)
在HttpPost:(正确)
After HttpPost: (Correct)
推荐答案
A &LT;选择多个=多个&GT;
回发值的数组(不是单值)。你财产选
必须的IEnumerable
,例如:
A <select multiple="multiple">
posts back an array of values (not a single value). You property Selected
needs to be IEnumerable
, for example
public string[] Selected { get; set; }
边注:由于文本和值的属性是一样的,你可以通过在列表
属性的SelectList
Side note: Since the text and value properties are the same, you can simplify you code by making the List
property SelectList
public SelectList List { get; set; }
和然后在控制器
model.List = new SelectList(new List<string>() { "1", "2", "3" })
(虽然它不清楚为什么你不会用 INT
)
这篇关于引导多选得到HttpPost选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!