asp.net mvc - MVC custom validation determine if edit or insert -


i have viewmodel want perform more custom validation on.

i have made viewmodel inherit ivalidatable , have validation in:

public ienumerable<validationresult> validate(validationcontext validationcontext) {     ...  } 

this works fine want validation creates , not edits. i'm thinking way of doing how determining within method whether edit or create.

is there way of doing or thinking aboout whole thing incorrectly?

use separate view model create , insert actions. if validation rules different, think worth use separate models anyway.

public class insertmyobjectviewmodel : ivalidatable {     [required]     public string name { get; set; }      // note lack of required attribute here     public string address { get; set; }      public ienumerable<validationresult> validate(validationcontext validationcontext)     {         ...     } } 

and separate edit

public class updatemyobjectviewmodel : ivalidatable {     [required]     public string name { get; set; }      [required]     public string address { get; set; }      public ienumerable<validationresult> validate(validationcontext validationcontext)     {         ...     } } 

this make sense if business rules dictate, example, customer's name upon registration not address. however, when user modifies account, business rules may require address. makes sense in lot of cases use 1:1 viewmodel:action ratio per object.

now, when write validate logic, becomes simpler. there may bit of duplication, it's easier modify when business rules change in future.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -