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

  • SEARCH
  • Home
  • 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 6370685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:57:44+00:00 2026-05-25T00:57:44+00:00

This question has been asked before in earlier versions of MVC. There is also

  • 0

This question has been asked before in earlier versions of MVC. There is also this blog entry about a way to work around the problem. I’m wondering if MVC3 has introduced anything that might help, or if there are any other options.

In a nutshell. Here’s the situation. I have an abstract base model, and 2 concrete subclasses. I have a strongly typed view that renders the models with EditorForModel(). Then I have custom templates to render each concrete type.

The problem comes at post time. If I make the post action method take the base class as the parameter, then MVC can’t create an abstract version of it (which i would not want anyways, i’d want it to create the actual concrete type). If I create multiple post action methods that vary only by parameter signature, then MVC complains that it’s ambiguous.

So as far as I can tell, I have a few choices on how to solve this proble. I don’t like any of them for various reasons, but i will list them here:

  1. Create a custom model binder as Darin suggests in the first post I linked to.
  2. Create a discriminator attribute as the second post I linked to suggests.
  3. Post to different action methods based on type
  4. ???

I don’t like 1, because it is basically configuration that is hidden. Some other developer working on the code may not know about it and waste a lot of time trying to figure out why things break when changes things.

I don’t like 2, because it seems kind of hacky. But, i’m leaning towards this approach.

I don’t like 3, because that means violating DRY.

Any other suggestions?

Edit:

I decided to go with Darin’s method, but made a slight change. I added this to my abstract model:

[HiddenInput(DisplayValue = false)]
public string ConcreteModelType { get { return this.GetType().ToString(); }}

Then a hidden automatically gets generated in my DisplayForModel(). The only thing you have to remember is that if you’re not using DisplayForModel(), you’ll have to add it yourself.

  • 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-05-25T00:57:45+00:00Added an answer on May 25, 2026 at 12:57 am

    Since I obviously opt for option 1 (:-)) let me try to elaborate it a little more so that it is less breakable and avoid hardcoding concrete instances into the model binder. The idea is to pass the concrete type into a hidden field and use reflection to instantiate the concrete type.

    Suppose that you have the following view models:

    public abstract class BaseViewModel
    {
        public int Id { get; set; }
    }
    
    public class FooViewModel : BaseViewModel
    {
        public string Foo { get; set; }
    }
    

    the following controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new FooViewModel { Id = 1, Foo = "foo" };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(BaseViewModel model)
        {
            return View(model);
        }
    }
    

    the corresponding Index view:

    @model BaseViewModel
    @using (Html.BeginForm())
    {
        @Html.Hidden("ModelType", Model.GetType())    
        @Html.EditorForModel()
        <input type="submit" value="OK" />
    }
    

    and the ~/Views/Home/EditorTemplates/FooViewModel.cshtml editor template:

    @model FooViewModel
    @Html.EditorFor(x => x.Id)
    @Html.EditorFor(x => x.Foo)
    

    Now we could have the following custom model binder:

    public class BaseViewModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var typeValue = bindingContext.ValueProvider.GetValue("ModelType");
            var type = Type.GetType(
                (string)typeValue.ConvertTo(typeof(string)),
                true
            );
            if (!typeof(BaseViewModel).IsAssignableFrom(type))
            {
                throw new InvalidOperationException("Bad Type");
            }
            var model = Activator.CreateInstance(type);
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
            return model;
        }
    }
    

    The actual type is inferred from the value of the ModelType hidden field. It is not hardcoded, meaning that you could add other child types later without having to ever touch this model binder.

    This same technique could be easily be applied to collections of base view models.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked before for earlier versions of Visual Studio
My apologies if this question has been asked before. I can see there are
This question has been asked before and there have been windows-specific answers but no
This question has been asked before in a more general way. I want to
This question has been asked before but in a slightly different way and I
This question has been asked before but never really answered (at least the way
This question has been asked before, but for older versions of GWT. I'm using
Perhaps this question has been asked before in a different way, but I haven’t
This question has been asked before a little over three years ago. There was
This question has been asked before, but not answered. Is there a list anywhere

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.