与 SelectList 的模型绑定
我使用 Html.DropDownList(string NameSelectListInViewData) 方法创建一个 DropDown。 这会生成具有正确值的有效选择输入。 一切都很好。
然而,提交后,源 SelectList 中的值并未绑定。
案例: ViewData.SearchBag.FamilyCodes:
public SelectList FamilyCodes { get; set; }
生成下拉列表的 Html:
<%=Html.DropDownList("SearchBag.FamilyCodes")%>
生成的 html:
<select id="SearchBag.FamilyCodes" name="SearchBag.FamilyCodes">
<option value=" ">Any</option>
<option value="A">BLUE</option>
<option value="B">BLACK</option>
<option value="C">BEIGE</option>
<option value="G">GREEN</option>
<option value="O">ORANGE</option>
<option value="P">PURPLE</option>
<option value="R">RED</option>
<option value="S">GRAY</option>
<option value="U">BROWN</option>
<option value="W">WHITE</option>
<option value="Y">YELLOW</option>
</select>
在我的控制器中,我有一个带有参数 searchBag 的操作。
public ActionResult AdvancedSearch(SearchBag searchBag) {
//Do Stuff with parameters in searchBag
return View("AdvancedSearch", searchViewData);
}
所有其他字段都可以很好地绑定,只有选择框不能。 有任何想法吗?
更新
对于未来的读者来说,阅读这篇博文可能是值得的:http://haacked.com/archive/0001/01/01/model-binding-to-a-list.aspx
I create a DropDown with the Html.DropDownList(string NameSelectListInViewData) method.
This generates a valid Select input with the correct values. And all is well.
Upon submit however, the value in the source SelectList is not bound.
Case:
ViewData.SearchBag.FamilyCodes:
public SelectList FamilyCodes { get; set; }
Html that generates the dropdown:
<%=Html.DropDownList("SearchBag.FamilyCodes")%>
Generated html:
<select id="SearchBag.FamilyCodes" name="SearchBag.FamilyCodes">
<option value=" ">Any</option>
<option value="A">BLUE</option>
<option value="B">BLACK</option>
<option value="C">BEIGE</option>
<option value="G">GREEN</option>
<option value="O">ORANGE</option>
<option value="P">PURPLE</option>
<option value="R">RED</option>
<option value="S">GRAY</option>
<option value="U">BROWN</option>
<option value="W">WHITE</option>
<option value="Y">YELLOW</option>
</select>
In my controller I have an action with a parameter searchBag.
public ActionResult AdvancedSearch(SearchBag searchBag) {
//Do Stuff with parameters in searchBag
return View("AdvancedSearch", searchViewData);
}
All other fields bind just fine, only the selection boxes don't.
Any ideas?
UPDATE
For future readers it might be worth it to read this blog post: http://haacked.com/archive/0001/01/01/model-binding-to-a-list.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
虽然“SearchBag.FamilyCodes”是有效的 HTML ID,但框架将尝试将每个表单项映射到同名的相应参数,并且参数名称中不能包含句点。 如果您为控件分配一个单独的 ID(Html.DropDownList 方法的不同重载),它会通过吗?
While "SearchBag.FamilyCodes" is a valid HTML id, the framework will attempt to map each form item to a corresponding parameter of the same name and you cannot have a period in a parameter name. If you assign a separate ID to the control (different overload of the Html.DropDownList method), does it come through?
SearchBag 在 html 中需要小写(searchBag 而不是 SearchBag)
SearchBag needs to be lower case in the html (searchBag rather than SearchBag)
我认为您需要将 FamilyCodes 的类型从 SelectList 更改为字符串。
然后提交时,FamilyCodes 的值应该是从下拉列表中选择的值。 我当前的项目中有几个这样的东西,没有任何问题。
I think you need to change the type of FamilyCodes from SelectList to string.
Then on submit the value of FamilyCodes should be the value selected from the dropdown list. I have several of these in my current project and have no problems.
昨天选择列表也有类似的问题,列表会生成正常,只是在 UpdateModel- 上会失败 =- 未绑定?
我在参数列表中找到了答案...
所以对我来说...
Country = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);
我使用 ViewModel 方法 - 并将其放在视图模型的构造函数中...
然后我获取控制器编辑操作中的值并将其分配回正在更新的对象...
Had a similar issue yesterday with select lists, the list would generate fine, just that on UpdateModel- would fail =- not bound?
And i found the answer in the parameters list...
So for me ...
Country = new SelectList(db.Countries, "pkCountry", "CountryName",address.fkCountry);
I use the ViewModel approach - and have this in the constructor of the view Model...
I then grab the values in the controllers Edit Action and assign back to the object that is updating...