c# - Set required fields based on HTTP Verb -


is possible set optional [required] attribute, applicable on patch or put. have following code no matter controller call required.

public class car {     [datamember(order = 0)]     public string carid { get; set; }     [datamember(order = 1)]     [required]     public string isincluded { get; set; } } 

controller;

[httppatch] public httpresponsemessage patchcar(car car) {     // check if submitted body valid     if (!modelstate.isvalid)     {         // bad!     } } 

what want following;

public class car {     [datamember(order = 0)]     public string carid { get; set; }     [datamember(order = 1)]     [required(patch = true, put = false]     public string isincluded { get; set; } } 

then modelstate take account.

i thought creating separate derived classes each action (verb), code becomes incredibly verbose.

this 1 of drawbacks of using data annotations validation unfortunately cannot conditionally added.

there number of options you...

  1. create separate models (or view models) each verb.
  2. look this.. http://andrewtwest.com/2011/01/10/conditional-validation-with-data-annotations-in-asp-net-mvc/ extends required ifrequired , adds conditional validation data annotations. (you need roll own should think , may clumsy!)

  3. try fluentvalidation. http://fluentvalidation.codeplex.com/ (this option depending on application requirements).

hope helps!


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -