asp.net - MVC serverside validation of single posted object -


i'm using single action handle 2 views use separate viewmodel so:

[httppost] public actionresult(privatecustomer p, corporatecustomer c) {   if(modelstate.isvalid) { ... } } 

my viewmodels this:

public abstract class customer {   public string name {get; set;}   public string username {get; set;}   ... } public class privatecustomer: customer {   ... } public class corporatecustomer: customer {   [required]   public new string name {get; set;} } 

this means as can use 1 url/action both (closely related) viewmodels. problem is, though, accept both viewmodels parameters post action, , model validation occur both (even though i'll use one).

given post privatecustomer, doesn't require name, i'll still validation errors on property.

i wondering if there's elegant way somehow prevent happening, preferably without manually removing errors modelstate.

the best thing if either of 2 objects validated.

thanks in advance suggestions.

this difficult in model due fact post data checked before can modify on server side. if did not want split action up, suggest dropping [required] data annotation in model , validating in controller this:

[httppost] public actionresult(privatecustomer p, corporatecustomer c) {       if (c != null)      {           if (c.name == null || c.name == "")           {                modelstate.addmodelerror("name", "name required.");           }      }       if(modelstate.isvalid) { ... }  } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -