6

This is very strange and I don't know why. I have a ViewModel that return some value for my object, when rendering it, they have different values, yet, they points to the same property:

<%: Model.myProperty %>

That returns "25", which is what I've set the property to be. But when rendered it as an textbox, it returned "0" as the value for my textbox!

<%: Html.TextBoxFor(f => f.myProperty) %>

Any idea why? The property is of decimal type. Thanks.

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
Saxman
  • 5,009
  • 11
  • 51
  • 72
  • Where and at what point do you set the value to 25? – Russ Cam Sep 02 '10 at 21:07
  • That code won't compile. Typo? Should be `<%= Html.TextBoxFor(f => f.myProperty) %>`. – djdd87 Sep 02 '10 at 21:10
  • I have some logic before that to determine the value for myProperty, then something like this: var model = new MyViewModel { myProperty = 25 }; When I run the debug, it clearly displaying myProperty = 25, and it reflects when rendering it as text, but for the textbox, somehow it didn't! Thanks. – Saxman Sep 02 '10 at 21:12
  • @GenericTypeTea, it's my typo posting here. Thanks. – Saxman Sep 02 '10 at 21:14
  • @Saxman - No problem... I think we might need to see your model and property here though. – djdd87 Sep 02 '10 at 21:22
  • @GenericTypeTea. What typo? <%: ... %> is .net 4 syntax. – Jesus Rodriguez Sep 03 '10 at 23:39
  • @Jesus Rodriguez - the op has edited the answer so there's no longer a typo. – djdd87 Sep 04 '10 at 08:12

3 Answers3

16

Try ModelState.Clear() before calling View or PartialView and passing in the model.

This issue was happening for me after doing a post. Its because the HTML helpers get their value from ModelState first before checking the actual model. Seems like this should be reversed IMO.

Jake Hoffner
  • 1,037
  • 9
  • 17
  • I agree, it's not desirable behavior in many scenarios. Here's another relevant question: http://stackoverflow.com/questions/1775170/asp-net-mvc-modelstate-clear. – Jordan Rieger Apr 24 '14 at 19:06
2

Having the same problem, I read this post today (and it’s solved my problem) and decided to investigate more regarding it. So, here is what I found:

“ASP.NET MVC assumes that if you are rendering a View in response to an HTTP POST, and you are using the Html Helpers, then you are most likely to be redisplaying a form that has failed validation. Therefore, the Html Helpers actually check in ModelState for the value to display in a field before they look in the Model.” By: Simon J Ince

If you want read more about this, access the link bellow: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

See you.

SAVJunior
  • 21
  • 1
0

My bad, seems like the problem is in the HTML, where I have the view rendered from multiple partial views, and some of the ID for the input are the same (I hide the edit view and rendered the main view using jQuery), somehow this textbox picks up the value from the view that was hidden!!! :)

Thanks all for looking.

Saxman
  • 5,009
  • 11
  • 51
  • 72