5

When I use a form html helper method in one of my views like <%=Html.Hidden("id", "some id text") %> it creates a hidden input field for me but it puts the wrong value in there.

Instead of getting

<input name="id" type="hidden" value="some id text"/>

I get

<input name="id" type="hidden" value="11000"/>

So the value is being found from somewhere else. In this case it's the primary id of the parent record. So it is an id, it's just the wrong id.

Does anyone have any ideas? I'm pretty sure this didn't happen in MVC1

dave
  • 1,344
  • 10
  • 16
Jero
  • 1,081
  • 1
  • 8
  • 11
  • 1
    Are you sure thats the value actually in the html, or is that the value you are getting back in your controller after a post? There's an explanation for the latter but if its the former it makes no sense. – jwsample Aug 11 '10 at 23:50
  • yeah it is the latter explanation. the controller action does indeed have an id param which gets the value 11000. What's the explanation? – Jero Aug 12 '10 at 22:35
  • View source on the generated page. Make sure the hidden field has the correct value. If it does have the correct value you have another input field named "id" that is overriding this one. All you need to do is deconflict the fields. – jwsample Aug 14 '10 at 21:52
  • No, it's not that I am looking at the wrong input field or accessing the wrong tag via js or something. It's that Html.Hidden() is using the wrong value. – Jero Aug 16 '10 at 00:04
  • 1
    Jero G, you may find http://stackoverflow.com/questions/4710447/asp-net-mvc-html-hiddenfor-with-wrong-value/4710911#4710911 helpful. It depends on whether your view is rendering a model that you've edited inside the action beforehand. – Lisa Mar 01 '11 at 06:49

2 Answers2

1

Model binding always takes precedence. The model binder doesn't know of if a field is hidden. See http://forums.asp.net/t/1559541.aspx and http://forums.asp.net/t/1703334.aspx

RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
0

I can thing about following options

  1. Value you are passing to the view is wrong, eg. data passed to view(you can check debugging in controller to see what are you passing into it
  2. You are using ViewData and TempData data with different values which are overridden

else please put here your code so we all can see what is wrong

cpoDesign
  • 8,953
  • 13
  • 62
  • 106