0

我的模型看起来像:

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 => m.Prop2) @Html.ValidationMessageFor(m => m.Prop2)

@Html.TextBoxFor(m => m.Prop1) @Html.ValidationMessageFor(m => m.Prop1)

Prop2我可以在提交时看到价值,但Prop1它是null. 任何想法?

4

1 回答 1

1

[DisplayFormat(ConvertEmptyStringToNull = false)]

这是你的答案。将此添加到代码中的每个字符串中,它将完美运行!

于 2013-02-22T06:40:40.970 回答