ASP.NET MVC 3 - Validation Question

后端 未结 4 472
生来不讨喜
生来不讨喜 2020-12-19 10:09

Good evening everyone I have a question regarding validation of drop-down list values. I have a view that is bound to a view model type called ReservationData.

4条回答
  •  情话喂你
    2020-12-19 10:37

    I had exactly the same problem with the field getting correctly validated in TextBoxFor but not in DropDownListFor.

    @Html.DropDownListFor(m => m.PaymentTO.CreditCardType,   Model.CreditCardTypeList, "Select Card Type", new { style = "width:150px;" }) 
    

    Since I had another DropDownListFor working on the same page, I knew that it wasn’t a generic DropDownListFor problem. I also have a complex model and parent object PaymentTO wasn’t initialized. When I set viewTO.PaymentTO = new PaymentTO(); in the Controller, the validation for the DropDownListFor started to work. So there is probably a problem with DropDownListFor, but the fix can be as simple as initializing the object in the controller.

提交回复
热议问题