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.

7
  • Where and at what point do you set the value to 25? Commented Sep 2, 2010 at 21:07
  • That code won't compile. Typo? Should be <%= Html.TextBoxFor(f => f.myProperty) %>. Commented Sep 2, 2010 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. Commented Sep 2, 2010 at 21:12
  • @GenericTypeTea, it's my typo posting here. Thanks. Commented Sep 2, 2010 at 21:14
  • @Saxman - No problem... I think we might need to see your model and property here though. Commented Sep 2, 2010 at 21:22

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree, it's not desirable behavior in many scenarios. Here's another relevant question: stackoverflow.com/questions/1775170/….
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.