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 6083911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:27:32+00:00 2026-05-23T11:27:32+00:00

I have a view that lists tables being added to a floor plan. Tables

  • 0

I have a view that lists tables being added to a floor plan. Tables derive from TableInputModel to allow for RectangleTableInputModel, CircleTableInputModel, etc

The ViewModel has a list of TableInputModel which are all one of the derived types.

I have a partial view for each of the derived types and given a List of mixed derived types the framework knows how to render them.

However, on submitting the form the type information is lost. I have tried with a custom model binder but because the type info is lost when it’s being submitted, it wont work…

Has anyone tried this before?

  • 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-23T11:27:33+00:00Added an answer on May 23, 2026 at 11:27 am

    Assuming you have the following models:

    public abstract class TableInputModel 
    { 
    
    }
    
    public class RectangleTableInputModel : TableInputModel 
    {
        public string Foo { get; set; }
    }
    
    public class CircleTableInputModel : TableInputModel 
    {
        public string Bar { get; set; }
    }
    

    And the following controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new TableInputModel[]
            {
                new RectangleTableInputModel(),
                new CircleTableInputModel()
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(TableInputModel[] model)
        {
            return View(model);
        }
    }
    

    Now you could write views.

    Main view Index.cshtml:

    @model TableInputModel[]
    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
        <input type="submit" value="OK" />
    }
    

    and the corresponding editor templates.

    ~/Views/Home/EditorTemplates/RectangleTableInputModel.cshtml:

    @model RectangleTableInputModel
    <h3>Rectangle</h3>
    @Html.Hidden("ModelType", Model.GetType())
    @Html.EditorFor(x => x.Foo)
    

    ~/Views/Home/EditorTemplates/CircleTableInputModel.cshtml:

    @model CircleTableInputModel
    <h3>Circle</h3>
    @Html.Hidden("ModelType", Model.GetType())
    @Html.EditorFor(x => x.Bar)
    

    and final missing peace of the puzzle is the custom model binder for the TableInputModel type which will use the posted hidden field value to fetch the type and instantiate the proper implementation:

    public class TableInputModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var typeValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".ModelType");
            var type = Type.GetType(
                (string)typeValue.ConvertTo(typeof(string)), 
                true
            );
            var model = Activator.CreateInstance(type);
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, type);
            return model;
        }
    }
    

    which will be registered in Application_Start:

    ModelBinders.Binders.Add(typeof(TableInputModel), new TableInputModelBinder());
    

    and that’s pretty much all. Now inside the Index Post action the model array will be properly initialzed with correct types.

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

Sidebar

Related Questions

I have a picker view that has a list of numbers to pick from.
We have created a new List View Style that shows thumbnails from a picture
I have a view that is joining two tables and ordering by the first
I have a view that has a list of jobs in it, with data
I have a list view that is periodically updated (every 60 seconds). It was
I have used a list view that uses a grid view in WPF. I
I have created a list view that displays the names and dates of items
I have a view that I want to add some custom drawing to. I
I have a View that can vary significantly, depending on the 'mode' a particular
I have a view that is loaded in the MainWindow.xib. It is just a

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.