I have a View that contains a ParentModel, which contains 2 Models.
I only want to validate the fields of one or the other. Not both.
Let´s say I have:
public ParentModel{
public BlueUser BlueUser {get; set;}
public GreenUser GreenUser {get; set;}
}
So the user selects either he is a GreenUser or a BlueUser. Each type of user has different fields, and depending which type of User the user selects, I want to only validate the fields on that specific type of user.
How can this be achieved?
EDIT:
Both forms/models must be on the same View, maybe with some JQuery or Partials?
Create one form with
HtmlHelper<BlueUser>and the other withHtmlHelper<GreenUser>, and make them post to different actions.First, the view model is:
Then use
HtmlHelperForto create an HtmlHelper for each of the submodels:Lastly, in the controller create a diferent POST action for each form:
Here’s the extension method: