Search code examples
asp.net-mvcvalidationasp.net-web-apiunobtrusive

MVC4 Web API server side validation display


Ref. this Microsoft's official video: http://www.asp.net/web-api/videos/getting-started/custom-validation

I downloaded the code and run it. It's fine.

Then, I remove all the client validation attributes(data-val-*) from the html file. It didn't work fine. I could not see the validation messages on the web page.

My question is how to regular the server side validation messages and how to display them as client-side validation.


Solution

  • Why would you delete the validation attributes? That's exactly what gets you the validation messages. To change the validation tests, you need to set the appropriate validation attributes on the model properties, e.g.,

    [Required]
    public string Genre { get; set; }
    
    [Range(1, 100)]
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
    
    [StringLength(5)]
    public string Rating { get; set; }
    

    As described in this post on ASP.NET MVC 4 Model Validation.