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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:01:29+00:00 2026-05-23T13:01:29+00:00

I have a model like so: return new MyViewModel() { Name = My View

  • 0

I have a model like so:

        return new MyViewModel()
        {
            Name = "My View Model",

            Modules = new IRequireConfig[]
            {
                new FundraisingModule()
                {
                    Name = "Fundraising Module",
                    GeneralMessage = "Thanks for fundraising"
                },

                new DonationModule()
                {
                    Name = "Donation Module",
                    MinDonationAmount = 50
                }
            }
        };

The IRequireConfig interface exposes a DataEditor string property that the view uses to pass to @Html.EditorFor like so:

    @foreach (var module in Model.Modules)
    {
        <div>
            @Html.EditorFor(i => module, @module.DataEditor, @module.DataEditor)  //the second @module.DataEditor is used to prefix the editor fields
        </div>
    }

When I post this back to my controller TryUpdateModel leaves the Modules property null. Which is pretty much expected since I wouldnt expect it to know which concrete class to deserialize to.

Since I have the original model still available when the post comes in I can loop over the Modules and get their Type using .GetType(). It seems like at this point I have enough information to have TryUpdateModel try to deserialize the model, but the problem is that it uses a generic type inference to drive the deserializer so it does not actually update any of the properties except the ones defined in the interface.

How can I get update my Modules array with their new values?

If any particular point isnt clear please let me know and I will try to clarify

  • 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-23T13:01:30+00:00Added an answer on May 23, 2026 at 1:01 pm

    You could use a custom model binder. Assuming you have the following models:

    public interface IRequireConfig
    {
        string Name { get; set; }
    }
    
    public class FundraisingModule : IRequireConfig
    {
        public string Name { get; set; }
        public string GeneralMessage { get; set; }
    }
    
    public class DonationModule : IRequireConfig
    {
        public string Name { get; set; }
        public decimal MinDonationAmount { get; set; }
    }
    
    public class MyViewModel
    {
        public string Name { get; set; }
        public IRequireConfig[] Modules { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Name = "My View Model",
                Modules = new IRequireConfig[]
                {
                    new FundraisingModule()
                    {
                        Name = "Fundraising Module",
                        GeneralMessage = "Thanks for fundraising"
                    },
                    new DonationModule()
                    {
                        Name = "Donation Module",
                        MinDonationAmount = 50
                    }
                }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    View:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.Name)
        for (int i = 0; i < Model.Modules.Length; i++)
        {
            @Html.Hidden("Modules[" + i + "].Type", Model.Modules[i].GetType())
            @Html.EditorFor(x => x.Modules[i])
        }
        <input type="submit" value="OK" />
    }
    

    and finally the custom model binder:

    public class RequireConfigModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var typeParam = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Type");
            if (typeParam == null)
            {
                throw new Exception("Concrete type not specified");
            }
            var concreteType = Type.GetType(typeParam.AttemptedValue, true);
            var concreteInstance = Activator.CreateInstance(concreteType);
            bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => concreteInstance, concreteType);
            return concreteInstance;
        }
    }
    

    which you would register in Application_Start:

    ModelBinders.Binders.Add(typeof(IRequireConfig), new RequireConfigModelBinder());
    

    Now when the form is submitted the Type will be sent and the model binder will be able to instantiate the proper implementation.

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

Sidebar

Related Questions

I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have a model name called StoreEntry. Django admin changes it to look like
Well i have a complex form view model like this : public class TransactionFormViewModel
I have a view model something like below, when onAfterRender is fired by Knockout
I have an action (Index) that return a View with a concrete model. Inside
If you have a controller method like so: @expose(json) def artists(self, action=view,artist_id=None): artists=session.query(model.Artist).all() return
I have a model like: CAMPAIGN_TYPES = ( ('email','Email'), ('display','Display'), ('search','Search'), ) class Campaign(models.Model):
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar

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.