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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:25:00+00:00 2026-05-26T06:25:00+00:00

I have the following classes: public class Truck { public Wheel Wheel { get;

  • 0

I have the following classes:

public class Truck {
      public Wheel Wheel { get; set; } 
}

public class Wheel {
      public int Number { get; set; }
}

And I registered the following model binder:

ModelBinders.Binders.Add(typeof(Wheel), new WheelModelBinder());

And:

public class WheelModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        throw new NotImplementedException();
    }
}

If I pass in:

public ActionResult(Wheel wheel) { ... }

The model binder gets hit and throws an exception. If I pass in

public ActionResult(Truck Truck) { ... }

The model binder doesn’t get hit.

In my application, every time Wheel goes in (whether or not it is nested within another type), I want the model binder to pick it up and manipulate the properties on wheel. What’s the best way to accomplish this?

Edit: Using EditorFor() correctly binds me, but I’m unable to arbitrarily edit the property. Using the above example:

public class WheelModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var valueProviderResult = bindingContext.ValueProvider.GetValue("Wheel.Number");
        return null;
    }
}

This will correctly get the wheel property. However, I might have a new, more complex object:

public class Cars {
    public class Truck { get; set; }
}

This breaks the ValueProvider and I will need to do, … GetValue("Truck.Wheel.Number") Am I abusing ModelBinder? Is there a better way to achieve my result (assume my result is to do an external lookup to make sure property Number is valid and if not, set it to something else).

  • 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-26T06:25:01+00:00Added an answer on May 26, 2026 at 6:25 am

    Actually, your ModelBinder and model will work as defined.

    The component tripping you up is the view. If you use the following:

    @model MyApplication5.Models.Truck 
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Truck</h2>
    
    @using (Html.BeginForm())
    {
        @Html.EditorFor(m => m.Wheel)
        <button type="submit">Submit</button>
    }
    

    then the view will generate the correctly named elements to be consumed by your custom ModelBinder.

    However, in this scenario, you do not need a custom ModelBinder, as the DefaultModelBinder will handle your nested model as long as MVC’s conventions are followed. Generally, a custom ModelBinder is for (1) a complex custom type or model, which the DefaultModelBinder cannot consume, or (2) where you are using a custom naming convention.

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

Sidebar

Related Questions

We have following classes public class MyPropertyBase { public int StartOffset { get; set;
I have the following classes: public class Person { public int Id {get; set;}
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have following classes public class Person { public string FirstName { get; set;
I have the following classes: public class BaseContainer { public BaseItem item {get; set;}
I have the following classes: public class Menu { public int Order { get;
I have the following classes [XmlRoot] public class AList { public List<B> ListOfBs {get;
I have the following classes in my Model: public abstract class Entity : IEntity
I have the following classes: public class Order { public string OrderName { get;
I have the following classes: public class DrawableComplexEntity2D { public List<GameComponent> Components { get;

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.