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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:00:02+00:00 2026-05-23T08:00:02+00:00

In my ASP.Net MVC3 project I have created a ModelBinder which binds a basemodel.

  • 0

In my ASP.Net MVC3 project I have created a ModelBinder which binds a basemodel. In my View i create a object from a Model that inherit from my basemodel. Now i wan´t to know which Model was created via reflection in my ModelBinder when i press the submit-button, but how?

ModelBinder:

public class MBTestBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        //need to know which Model was created -> convert into the right object
        //reflection?
    }
}

Models:

[ModelBinder(typeof(MBTestBinder))]
public class MBTest
{
    public string Name { get; set; }
    public MBTest()  {}
}

public class MBAbl : MBTest
{
    public MBAbl()  {}
    public string House { get; set; }
}

View:

@model ModelBinderProject.Models.MBTest

@using (Html.BeginForm("Index", "Home")) {
<fieldset>
    <div class="editor-field">
        @Html.EditorForModel(Model)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>

Controller:

public ActionResult Create(MBTest testItem)
{
    //on init get a view from a class that hast inherit the class MBTest
    if (testItem.Name == null ) testItem = new MBAbl();

    return View(testItem);
}

edit:

with bindingContext.ValueProvider.GetValue("House") i can get the value of the Form but bindingContext.ModelType thinks that my Model is MBTest

  • 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-23T08:00:03+00:00Added an answer on May 23, 2026 at 8:00 am

    Finaly i solved it with the workaround of carrying the name of the model in my model and dynamically create the right model in the modelbinder.
    If you know a better solution plz show me 🙂

    HomeController:

    // CREATE
    public ActionResult About(MBTest testItem)
    {
        if (testItem == null)
        {
            testItem = new MBAbl();
            testItem.Model = "MBAbl";
        }
    
        return View(testItem);
    }
    

    Models:

    public class MBTest
    {
        public MBTest()  {}
    
        [HiddenInput]
        public string Model { get; set; }
    
        public string Name { get; set; }
    }
    
    public class MBAbl : MBTest
    {
        public MBAbl()  {}
    
        public string House { get; set; }
    }
    
    public class MBAb2 : MBTest
    {
        ...
    }
    

    ModelBinder:

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext == null) throw new ArgumentNullException("controllerContext");
        if (bindingContext == null) throw new ArgumentNullException("bindingContext");
    
        //string 'Model' is needed in the base class
        var modelType = bindingContext.ValueProvider.GetValue("Model");
    
        if (modelType != null && !string.IsNullOrEmpty(modelType.AttemptedValue))
        {
            string projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
    
            Type classtype = Type.GetType(string.Format("{0}.Models.{1}", projectName, modelType.AttemptedValue));
            PropertyInfo[] properties = classtype.GetProperties();
    
            var classObject = classtype.GetConstructor(new Type[] { }).Invoke(null);
    
            foreach (PropertyInfo propertie in properties)
            {
                var value = bindingContext.ValueProvider.GetValue(propertie.Name).AttemptedValue;
                classtype.GetProperty(propertie.Name).SetValue(classObject, value, null);
            }
    
            return classObject;
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with create ulr routing for asp.net mvc3 application. My project has
I have MVC3 asp.net project. I create controller function with some parameters, the question
I have an asp.net mvc3 project, it has some reports in aspx web pages.
I have an ASP.NET MVC3 project that uses a tab strip on certain pages.
I have some C# code in my ASP.NET MVC3 project that's throwing an exception
I have an ASP.NET MVC 3 project that I just created using the project
I have an ASP.NET MVC3 project. I want to insert some <td> 's in
I am currently working on an ASP.NET MVC3 project in Visual Studio. I have
I have an ASP.Net MVC3 project that calls a WCF service to get data.
I have troubles consuming unity container in ASP.NET MVC3 application. I have several project

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.