Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC 2.0 Html.HiddenFor HtmlHelper extension doesn't return the Value

We're trying to be type-safe in our views and use the new ExpressionInputExtenssion HtmlHelpers, but we are seeing some inconsistent results. We have a view that looks like this:

ViewData.Model.FooID = <%= ViewData.Model.FooID %><
Model.FooID = <%= Model.FooID  %>       
<%= Html.HiddenFor(x=>x.FooID) %>  

But what we see in the rendered view is this:

ViewData.Model.FooID = 515b0403-e75b-4bd7-9b60-ef432f39d338
Model.FooID = 515b0403-e75b-4bd7-9b60-ef432f39d338    
<input id="FooID" name="FooID" type="hidden" value="" />  

I can manually add this:

<input id="FooID" name="FooID" type="hidden" value="<%= Model.FooID %>" />

But now we are no longer, but surprisingly when I do, The Html.HiddenFor always has the correct value.

like image 650
Mark Avatar asked Nov 12 '09 19:11

Mark


People also ask

What is the use of Html HiddenFor in MVC?

HiddenFor() The Html. HiddenFor<TModel, TProperty> extension method is a strongly typed extension method generates a hidden input element for the model property specified using a lambda expression. Visit docs.microsoft.com to know all the overloads of HiddenFor() method.

How does Html HiddenFor work?

It creates a hidden input on the form for the field (from your model) that you pass it. It is useful for fields in your Model/ViewModel that you need to persist on the page and have passed back when another call is made but shouldn't be seen by the user.

What is HtmlHelper?

An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.


2 Answers

Take a look at this: http://blogs.msdn.com/b/simonince/archive/2010/05/05/asp-net-mvc-s-html-helpers-render-the-wrong-value.aspx

like image 73
Edgarin Avatar answered Sep 28 '22 03:09

Edgarin


It looks like the model binder that is behind the extension method cannot convert you FoodID datatype to a string. Is your data type a regular GUID?

I known there are overloads for this extension method for working with binary but I'm not sure about GUIDs ....

Have u tried debbuging it?

like image 24
Andre Gallo Avatar answered Sep 28 '22 04:09

Andre Gallo