【发布时间】:2014-03-31 19:59:05
【问题描述】:
我有一个具有TestStr 和TestBool 作为属性的视图模型。如果我提交表单,只有TestStr 值被设置为绑定视图模型,而不是TestBool。 TestBool 的表单集合中的值设置为 "on",这是问题所在吗?是否应该将其设置为true 才能将其标记为true?
我的 c# 视图模型:
public class MyViewModel {
public bool TestStr { get; set; }
public bool TestBool { get; set; }
}
我的剃刀视图,在表单内:
<input name="TestStr" value="this is getting set in my MVC action" />
<input name="TestBool" checked=checked /> // this always gets set to false, even tho it's checked
我的 MVC 操作:
public ActionResult(MyViewModel vm) {
// vm.TestBool is always false
}
我注意到,如果我使用@Html.CheckBoxFor(...),它会在我的 MVC 操作中正确绑定它。是否可以使用输入的 html 来执行此操作?只是因为我有一些正在使用的集合,所以我必须为每个集合提供输入名称。
【问题讨论】:
标签: c# asp.net-mvc formcollection