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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:12+00:00 2026-06-18T03:49:12+00:00

I have the following ViewModels : public class MyViewModel { public BaseViewModel mySubViewModel; }

  • 0

I have the following ViewModels :

public class MyViewModel
{
  public BaseViewModel mySubViewModel;
}

public class ChildViewModel: BaseViewModel
{}

I then create a MyViewModel model, which contains a property of type ChildViewModel. In the View, it is displayed just fine.

Then I hit the save button to submit changes to my Model and I call the following controller:

    [HttpPost]
    public ActionResult Edit(MyViewModel model)
    {
        return null;
    }

To my surprise, the property mySubViewModel is now of type BaseViewModel instead of ChildViewModel ! I have no idea what’s going on here. What am I doing wrong ?

  • 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-18T03:49:13+00:00Added an answer on June 18, 2026 at 3:49 am

    To my surprise, the property mySubViewModel is now of type
    BaseViewModel instead of ChildViewModel ! I have no idea what’s going
    on here. What am I doing wrong ?

    You shouldn’t be surprised by that. You are doing nothing wrong. This is by design. The default model binder sees that your controller action is taking a MyViewModel argument and is attempting to bind the contents of the POST request to it. The default model binder has absolutely no way of knowing that you might have written some derived classes (such as ChildViewModel in your case) and that you want those derived classes to be used instantiated here. The fact that you have passed this concrete view model instance from your GET action to the view has no influence to the POST action. Think of it in terms of HTTP and what the default model binder sees. Think for example that this POST action could be invoked from some completely different client and not from a form submission. Could be for example an iPhone application performing an HTTP request to the POST action. See, now it makes perfect sense. The default model binder can only see the payload of the POST request and the type you specified as action argument. And that’s what he does => it instantiates this types and binds its properties from the POST payload data.

    So one possibility is to write a custom model binder that will instantiate the concrete instance of your view model you wish. I have exemplified such a custom model binder at this post. In this example I have included a hidden field inside the form that will contain the concrete type of the view model that we would like to be instantiated and then the custom model binder simply uses this information to create this type at runtime.

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

Sidebar

Related Questions

Why doesn't my viewModel get inherited? I have the following viewModels: public class BaseViewModel
I have the following Models and ViewModels (edited for brevity): public class Advert {
I have the following ViewModel, which is the base class of other ViewModels that
I have the following class used for custom validation: [AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)] public sealed
I have the following model : public class Foo { [Key] public int FooID
I have the following ViewModel public class RecommendationModel { public List<CheckBoxItem> CheckBoxList { get;
I'm using ASP.NET MVC and I have a model class which represents a peice
I have got the following Models: public class Car : BindableBase { private string
I have an ASP.NET MVC3 application. I have the following ViewModel : public class
I have the following ViewModel: public class TransportationUnit : ViewModelBase { private string _TypeOfFuel;

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.