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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:56:36+00:00 2026-05-15T07:56:36+00:00

This seems like the model binding is causing me issues. Essentially I have a

  • 0

This seems like the model binding is causing me issues.

Essentially I have a model called ProductOption and for the purpose of this question it has 2 fields

ID (Int) PK
ProductID (Int) FK

I have a standard route set-up

    context.MapRoute(
        "Product_default",
        "Product/{controller}/{action}/{id}",
        new { controller = "Product", action = "Index", id = UrlParameter.Optional }
    );

and if the user wants to add an option the URL is,

/Product/Options/Add/1

in the above URL 1 is the ProductID, I have the following code to return a blank model the the view,

[HttpGet]
public ActionResult Add(int id)
{
    return View("Manage", new ProductOptionModel() { ProductID = id });
}

Now in my view I keep a hidden field

<%= Html.HiddenFor(x=>x.ID) %>

This is used to determine (on submit) if we are editing or adding a new option. However the Model binder in .net seems to replace .ID (Which was 0 when leaving the above get actionresult) with 1 (or the value of the id parameter in the URL)

How can I stop or work around this?

ViewModel

public class ProductExtraModel
{
    //Database 
    public int ID { get; set; }
    public string Name { get; set; }
    public int ProductID { get; set; }

    public ProductModel Product { get; set; }
}
  • 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-15T07:56:36+00:00Added an answer on May 15, 2026 at 7:56 am

    I think the id parameter is being set because by default, your route sets it. Inside your controller, you’re setting an additional parameter in your ViewModel of ProductID, this will probably always equal the ID parameter as both are basically being set to the QueryString / GET parameter. (1 in this case).

    Your fix of changing the route works as you stop it from allocating the ID parameter seems a good choice, but maybe not ideal – depending on how you want to solve the issue:

    context.MapRoute(
      "Product_addoptionalextra",
      "Product/{controller}/Add/{ProductID}",
      new { controller = "Product",action="Add", ProductID = UrlParameter.Optional }
    );
    

    Alternatively, re-arrange your variables so that ID is actually the relevant ProductID, then you could have OtherID which represents ID.

    The way I would maybe suggest to fix this problem if you have MVC 2, is to use EditorTemplates / DisplayTemplates. Although I don’t know your ProductViewModel, I assume it has the ID within it. If you set the appropriate template, you can almost forget about the potentially overlapping IDs.

    public class ProductExtraModel
    {
       //Database 
      public int ID { get; set; }
      public string Name { get; set; }
    
      [UIHint("Product")]
      public ProductModel Product { get; set; }
    }
    

    You’ll be able to access the product ID when the model is passed back into the controller using productExtraViewModel.Product.ID and your normal ID will still be available on productViewModel.Id.

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

Sidebar

Ask A Question

Stats

  • Questions 468k
  • Answers 468k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's perfectly legal and acceptable. However, some notes: Your destructor… May 16, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer To be honest, seeing as your dealing with a finite… May 16, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer import threading def printit(): threading.Timer(5.0, printit).start() print "Hello, World!" printit()… May 16, 2026 at 2:13 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.