ASP.NET MVC3 Validation of nested view model object fields?

As far as I know you can't have client side validation on nested objects. And a quick google search seems to confirm that.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a view model that looks like this: public class VenueIndexViewModel : BaseViewModel { public VenueAddViewModel Venue; ... } public class VenueAddViewModel { ... Required(ErrorMessage = "This field is required") public string State { get; set; } ... } In my view, I'm rendering a form with with a drop down list for this property like so: using (var form = Html.BeginForm()) { ... @Html. DropDownListFor(x => x.Venue. State, Model.GetStates()) @Html.

ValidationMessageFor(x => x.Venue. State) ... } This works, but the problem is that the the Required attribute on the view model appears to be ignored. If I look at the HTML, the data-val-* attributes are missing as well.

... However, if I change the rendering to a textbox... using (var form = Html.BeginForm()) { ... @Html. TextBoxFor(x => x.Venue. State) @Html.

ValidationMessageFor(x => x.Venue. State) ... } I see the expected data-val-* attributes and the validation works: I should note that I have other view models elsewhere that use DropDownListFor with a flat view model (no nested objects) and the validation works fine there, so I'm thinking I've hit a bug in the MVC validation handling for drop down lists when using a nested view model. Can anyone confirm / advise?

Asp.net-mvc-3 link|improve this question asked Jan 17 at 18:28Chris9,98522472 76% accept rate.

I just confirmed that if I move the form code to a partial view and pass it the exact view model it needs (VenueAddViewModel instead of VenueIndexViewModel), the validation attributes appear fine. However, I'm still not sure if I was doing something incorrectly above or if I've really encountered a bug that should be reported. – Chris Jan 17 at 18:35.

As far as I know you can't have client side validation on nested objects. And a quick google search seems to confirm that. forums.asp.net/t/1737269.aspx/1.

1 That's fine and dandy, but it DID work for plain text fields. I'm guessing that was accidental, and support was never officially coded for nested objects, so what I perceived as a bug is really just an untested / unsupported scenario. – Chris Jan 17 at 19:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions