I have two questions regarding the data annotation validation.
1. Can you change the look og how the error message i show. Maybe use some of my own html
2. Can i use data annotation validation with viewmodels? I know that i can put data annotation on the attributes of my viewmodels but is it the way to go?
Hope someone can help
Yes, in Viewmodels you can implement the INotifyableObject interface. MVC will call this implementation just before the post back enters the controller to validate anything you want. There are two modes to validation 1) For the Models itself using data
annotations and 2) And any custom business rules you need to implement in the View model. To notify the view, the ViewModel validate method will return a collection of <ViewResult> if you use the "Yield return new ViewResults("blah, blah blah")" construct
you can write as many validation rules as you need an very easily send them to the view.
Reminder, make sure you set up something like @Html.ValidationSummry(true or false) up in the view in order to see them. One final note, any Action method checking for Model.ViewState is valid any bypassing logic, will force those errors to be fixed! In
other words it is a controller decision on what to do, however, why set up exception parsing rules for this if you just need to warn the user of something rather than forcing a correction. For warning messages you are better off implementing on client side.
Can you change the look? Anything is possible if you implement the entire system yourself or use JavaQuery to manipulate the DOM after document.ready. You’ll easily be able to spot what MVC does to generate those messages. Jquery can do anything with
And then i want to use entity frameworks serverside validation.
I want the same error to show up both for client side errors and for server side errors
I want to validate with the annotation from my business classes.
I have viewmodels.
How do you handle these to validation?
Can you change the look og how the error message i show. Maybe use some of my own html
I suggest that you could specify the style in the parent element, such as:
<div style="..."> @Html.ValidationMessageFor(m => m.UserName) </div>
You also could custom HtmlHelper to custom it.
# Custom ValidationSummary template Asp.net MVC
Can i use data annotation validation with viewmodels? I know that i can put data annotation on the attributes of my viewmodels but is it the way to go?
You just need add the data annotation validation attribute to the fields or properties of the
1. Use CSS style, what you want.
2. Custom Data Annotation always good, beco’z you can push your logic into validation.