我的模型看起来像:
public class MyViewModel
{
public MyViewModel()
{
MyClasses = new List<MyClass>();
}
public List<MyClass> MyClasses{ get; set; }
[Required(ErrorMessage = "is required.")]
public string Prop1{ get; set; }
[Required(ErrorMessage = "is required.")]
public int Prop2{ get; set; }
}
我的控制器发帖:
[HttpPost]
public ActionResult MyActionOnPost(MyViewModel model)
{
//save to DB
//Prop2 is not null but Prop1 is nulll. why??
}
查看代码:
@ Html.TextBoxFor(m =&gt; m.Prop2)@ Html.ValidationMessageFor(m =&gt; m.Prop2)
@ Html.TextBoxFor(m =&gt; m.Prop1)@ Html.ValidationMessageFor(m =&gt; m.Prop1)
我可以在提交时看到Prop2
值,但Prop1
是null
。有什么想法吗?
答案 0 :(得分:1)
[DisplayFormat(ConvertEmptyStringToNull = false)]
这是你的答案。将其添加到代码中的每个字符串,它将完美运行!