Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use DataAnnotations to override the default error messages in MVC, as well as provide your own validation for whatever fields you need to. See the following:</p> <p><a href="http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx</a></p> <p><a href="http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs" rel="nofollow noreferrer">http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs</a></p> <p>If you are using EF, you will need to create a MetaData class off the EF generated class and then add the data annotations. The end of the 2nd article covers how to write these classes for entity framework.</p> <p>In your specific case you will want to do something like:</p> <pre><code>using System.ComponentModel.DataAnnotations; public class Person { public string FirstName {get;set;} [Range(0, 110, ErrorMessage = "&lt;your error message&gt;")] public int Age {get;set;} } </code></pre> <p><strong>UPDATE</strong> I did forget one thing that is easy to overlook, you need to include the following JS files for the data annotations to be picked up client side without having to do a post:</p> <ul> <li><p>MicrosoftAjax.js</p></li> <li><p>MicrosfotMvcValidation.js</p></li> </ul> <p>These should be stock in the Scripts folder of your project (if you are using MVC 2), and you need to include them either on your page or in your master page, </p> <pre><code>&lt;script src="&lt;%= Url.Content("~/Scripts/MicrosoftAjax.js") %&gt;" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="&lt;%= Url.Content("~/Scripts/MicrosoftMvcValidation.js") %&gt;" type="text/javascript"&gt;&lt;/script&gt; </code></pre> <p>you will also need to include </p> <p><code>&lt;% Html.EnableClientValidation(); %&gt;</code> </p> <p>on the page in question.</p> <p>The client side validation will not let you submit the form until all fields meet validation requirements, as Kaspars Ozols points out you will still need to call Model.IsValid on your controller.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload