views:

124

answers:

1

I'm trying to do this simple thing

<%= Html.HiddenFor(model => model.Id)%>

the model is

[HiddenInput(DisplayValue=true)]
public int Id { get; set; }

but i always get this rendered

<input type="hidden" value="0" name="UserInfo.Id" id="UserInfo_Id">

i've check and the id is NOT 0.. ?!

need some explanation here...

Edit

The problem seem's to be the post thing mentionned below. This is working

<input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />

Thanks to Manaf

A: 

I'm not sure if this is the case with you but the Html.HiddenFor() "do not output correct values after a post if the value is changed during the post." and this is a not a bug it was designed that way.

More Info Here

Quick Fix :

Don't use the helper, try this instead :

<input type="hidden" value="<%= Html.AttributeEncode(model.Id) %>" id="Id" name="Id" />

Always worked for me :)

Manaf Abu.Rous
Thanks that was the case.
mateo
in fact we should do this to handle complex model <input type="hidden" value="<%= Html.AttributeEncode(Model.Id) %>" id="<%= Html.IdFor(model=>model.Id)%>" name="<%= Html.NameFor(model=>model.Id)%>" />
mateo
Glad i could help :)
Manaf Abu.Rous