MVC Client Validation on InputModel rather than ViewModel?

The way client validation works out of the box is that it uses the ModelMetadata of the view model passed to the view to emit HTML5 data attributes on the input fields based on Data Annotation attributes you have used on your view model. If you are using a completely different view model as action argument in your POST action this obviously won't work as server side validation will rely upon the view model in the POST action while client validation will rely upon the view model passed to the view rendering this form The only solution I can see to this problem is to forget about automatic client validation and define the jquery. Validate rules manually so that they are coherent with the view model you are using in the POST action on the server.So you can basically forget about the data attributes, exclude the jquery.validate.unobtrusive.min.

Js script from your page and get into coding : $(function() { $('form'). Vaildate({ rules: { ... }, messages: { ... } }); }).

The way client validation works out of the box is that it uses the ModelMetadata of the view model passed to the view to emit HTML5 data-* attributes on the input fields based on Data Annotation attributes you have used on your view model. If you are using a completely different view model as action argument in your POST action this obviously won't work as server side validation will rely upon the view model in the POST action while client validation will rely upon the view model passed to the view rendering this form. The only solution I can see to this problem is to forget about automatic client validation and define the jquery.

Validate rules manually so that they are coherent with the view model you are using in the POST action on the server. So you can basically forget about the data-* attributes, exclude the jquery.validate.unobtrusive.min. Js script from your page and get into coding: $(function() { $('form').

Vaildate({ rules: { ... }, messages: { ... } }); }).

Thanks makes sense. I've just tried to extend the viewmodel to include a new User() class which is what I was posting back and changed the textboxes to TextBoxFor( x=>x.User. Forename) but I still don't get any client validation.

UPDATE: Using BeginForm seems to create the data attributes – Jon Jun 3 at 13:24.

The way client validation works out of the box is that it uses the ModelMetadata of the view model passed to the view to emit HTML5 data-* attributes on the input fields based on Data Annotation attributes you have used on your view model. If you are using a completely different view model as action argument in your POST action this obviously won't work as server side validation will rely upon the view model in the POST action while client validation will rely upon the view model passed to the view rendering this form. The only solution I can see to this problem is to forget about automatic client validation and define the jquery.

Validate rules manually so that they are coherent with the view model you are using in the POST action on the server. So you can basically forget about the data-* attributes, exclude the jquery.validate.unobtrusive.min.

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