Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:29:22+00:00 2026-06-06T17:29:22+00:00

I have a View that contains a ParentModel, which contains 2 Models. I only

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:29:25+00:00Added an answer on June 6, 2026 at 5:29 pm

    Create one form with HtmlHelper<BlueUser> and the other with HtmlHelper<GreenUser>, and make them post to different actions.

    First, the view model is:

    @model ParentModel
    

    Then use HtmlHelperFor to create an HtmlHelper for each of the submodels:

    @{
       var blueHtml = Html.HtmlHelperFor(Model.BlueUser);
       var greenHtml = Html.HtmlHelperFor(Model.GreenUser);
    }
    
    @using (blueHtml.BeginForm("BluePost", null)) {
       @blueHtml.EditorForModel()
    }
    
    @using (greenHtml.BeginForm("GreenPost", null)) {
       @greenHtml.EditorForModel()
    }
    

    Lastly, in the controller create a diferent POST action for each form:

    [HttpPost]
    public ActionResult BluePost(BlueUser model) {
       ...
    }
    
    [HttpPost]
    public ActionResult GreenPost(GreenUser model) {
       ...
    }
    

    Here’s the extension method:

    public static class HtmlHelperFactoryExtensions {
    
       public static HtmlHelper<TModel> HtmlHelperFor<TModel>(this HtmlHelper htmlHelper) {
          return HtmlHelperFor(htmlHelper, default(TModel));
       }
    
       public static HtmlHelper<TModel> HtmlHelperFor<TModel>(this HtmlHelper htmlHelper, TModel model) {
          return HtmlHelperFor(htmlHelper, model, null);
       }
    
       public static HtmlHelper<TModel> HtmlHelperFor<TModel>(this HtmlHelper htmlHelper, TModel model, string htmlFieldPrefix) {
    
          var viewDataContainer = CreateViewDataContainer(htmlHelper.ViewData, model);
    
          TemplateInfo templateInfo = viewDataContainer.ViewData.TemplateInfo;
    
          if (!String.IsNullOrEmpty(htmlFieldPrefix))
             templateInfo.HtmlFieldPrefix = templateInfo.GetFullHtmlFieldName(htmlFieldPrefix);
    
          ViewContext viewContext = htmlHelper.ViewContext;
          ViewContext newViewContext = new ViewContext(viewContext.Controller.ControllerContext, viewContext.View, viewDataContainer.ViewData, viewContext.TempData, viewContext.Writer);
    
          return new HtmlHelper<TModel>(newViewContext, viewDataContainer, htmlHelper.RouteCollection);
       }
    
       static IViewDataContainer CreateViewDataContainer(ViewDataDictionary viewData, object model) {
    
          var newViewData = new ViewDataDictionary(viewData) {
             Model = model
          };
    
          newViewData.TemplateInfo = new TemplateInfo { 
             HtmlFieldPrefix = newViewData.TemplateInfo.HtmlFieldPrefix 
          };
    
          return new ViewDataContainer {
             ViewData = newViewData
          };
       }
    
       class ViewDataContainer : IViewDataContainer {
    
          public ViewDataDictionary ViewData { get; set; }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that contains two NSTextFieldCell s. The size at which these
I have created one custom view that contains the horizontalscrollview. Now when i changes
I have created one custom view that contains the horizontalscrollview. Now when i changes
I have a view that contains a UITableView and a UILabel which works perfectly
I have a list view that contains some data, I want to share it
I have one grid view that contains some template columns. In this, the first
Lets say I have a partial view that contains both a checkbox and a
I have a view model that contains a Product class type and an IEnumerable<
I'm developing an iOS 4 application. I have a main view that contains another
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.