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

  • Home
  • SEARCH
  • 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 8136641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:49:31+00:00 2026-06-06T10:49:31+00:00

I’ve stumbled upon a really weird situation with ASP.Net MVC, passing an id as

  • 0

I’ve stumbled upon a really weird situation with ASP.Net MVC, passing an “id” as an action parameter and an “id” hidden form element. I have an action that has an “id” parameter. This id value represents a project. I have a controller action where I’m creating a data entry form for assigning an employee to the passed in project (I create a drop down list where administrator selects an employee to be assigned to passed project). By assigning an employee to a project, I create a ProjectEmployee record (project id, employee id, and an id that represents the combination which is an identity column in the database). The identity column (which is named “id”) is also a hidden form element. This is necessary because I need to be able to edit the project/employee assignment at a later date.

Anyways, when creating a new employee assignment to a project, the project id (which is the “id” being passed to the action) is being applied to the “id” hidden form element.

I’m passing 117 into the action. 117 is the projectId. It’s being set to the “id” hidden field which should be 0 because 0 represents a new project/employee assignemnt.

View Model

id  - unique id that represents the Project/Employee combination
projectId - the "Id" being passed to action
EmployeeId - what the admin is selecting from drop down
rate
startdate
endDate
...

Data Entry Form

@Html.HiddenFor(m => m.Id) 
@Html.HiddenFor(m => m.ProjectId)
...
Id: @Model.Id <br />
ProjectId: @Model.ProjectId<br />

So, @Html.HiddenFor(m => m.Id) renders a hidden form element with a value of 117. @Model.Id renders 0 to the UI. Stepping through the code I can visually see the value of the Id property to be 0. How come HiddenFor is getting it’s wires crossed and pulling the value of 117?

This bug has made it’s way into production so I have a mess on my hands with data getting messed up because instead of creating a new record in the database table, I’m actually UPDATING existing records because the “Id” property is erroneously getting set from 0 (which represents a new record) to 117 (which is the projectId) and therefore am updating a different record.

  • 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-06T10:49:34+00:00Added an answer on June 6, 2026 at 10:49 am

    How come HiddenFor is getting it’s wires crossed and pulling the value of 117?

    That’s by design. All HTML helpers such as TextBoxFor and HiddenFor first use the value of ModelState when binding and then the value of the model. I presume your controller action looks like this:

    public ActionResult Foo(int id)
    {
        SomeModel model = ...
        // At this stage model.Id = 0
        return View(model);
    }
    

    The thing is that the default model binder adds a value into the ModelState with the key id which is used by the helper and the model property value is ignored. This is not a bug. It is how the HTML helpers are designed. It’s a bit confusing in the beginning when you don’t know it, but once you get accustomed to this behavior you don’t fall in the trap a second time.

    One possibility to fix the problem is to remove this value from ModelState:

    public ActionResult Foo(int id)
    {
        ModelState.Remove("id");
        SomeModel model = ...
        // At this stage model.Id = 0
        return View(model);
    }
    

    Or rename the Id property inside your model to something else to avoid the conflict.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.