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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:20:16+00:00 2026-05-12T00:20:16+00:00

I have the following route: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL with

  • 0

I have the following route:

            routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

and I use the ViewModel:

namespace MvcApplication1.Models
{
    public class ProductViewModel
    {

        public ProductViewModel()
        {
            ProductDetail = new ProductInfo();
        }

        public ProductInfo ProductDetail { get; set; }

    }

    public class ProductInfo
    {
        public string Name { get; set; }
        public int ProductID { get; set; }
    }

}

I need a way to bind the parameter id of the route to Model.ProductDetail.ProductID.

/Products/Display/2 should lead to :

Model.ProductDetail.ProductID == 2

I know this looks a bit strange: It would be much simpler if my ViewModel would only be

public class ProductViewModel{ public int Id {get;set;}}

For handling partials I prefer to use aggregation. I realy cant have ID in the main ViewModel class.

I am pretty shure that I need to implement my own ModelBinder, but I dont see where I should implement my own rules.

How can I map the route parameter “id” to the property Model.ProductDetail.ProductID ?

EDIT:

Here is how its done –

    public class ProductModelBinder: DefaultModelBinder
{
    protected override void BindProperty(
        ControllerContext controllerContext,
        ModelBindingContext bindingContext, 
        System.ComponentModel.PropertyDescriptor propertyDescriptor
        )    {
            if (bindingContext.ModelType == typeof(ProductViewModel)
            && String.Compare(propertyDescriptor.Name, "ProductDetail", true) == 0)     
            {         
                int id = int.Parse((string)controllerContext.RouteData.Values["id"]);
                ProductViewModel productView = bindingContext.Model as ProductViewModel;
                productView.ProductDetail.ProductID = id;           
                return;       
         }       
        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);    }
}
  • 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-12T00:20:16+00:00Added an answer on May 12, 2026 at 12:20 am

    Try something like this (not tested):

    public class CustomModelBinder : DefaultModelBinder
    {
        protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        {
            if (bindingContext.ModelType == typeof(ProductInfo)
                && String.Compare(propertyDescriptor.Name, "ProductID", true) == 0)
            {
                var id = (int)controllerContext.RouteData.Values["id"];
    
                var productInfo = bindingContext.Model as ProductInfo;
    
                productInfo.ProductID = id;
    
                return;
            }
    
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following routing routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL
I have the following route: routes.MapRoute( Default, // Route name {language}/{controller}/{action}/{id}, new { language
I have the following default and only route: routes.MapRoute( Default, // Route name {controller}/{action}/{id},
Let's say I have the following rule routes.MapRoute( Default, // Route name {controller}/{action}/{id}, //
I have the following URl: http://localhost:12981/BaseEvent/EventOverview/12?type=Film This is route: routes.MapRoute( Default, // Route name
I have the following Route defined in Global.asax: routes.MapRoute( IncidentActionWithId, // Route name Incidents/{companyId}/{action}/{id},
HI I have the following routing table routes.MapRoute(null, Save, // Route name new {
Why doesn't this work? Route: routes.MapRoute( Summary, {controller}/{id}, new { controller = Summary, action
I have the following routes defined for my application: routes.MapRoute( Referral, // Route name
I have the following route's defined: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute({resource}.axd/{*pathInfo}); routes.MapRoute(

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.