4

i am using HiddenFor with model binding which is binding value to it. i want to reset the binded value to zero.How can i do it?

i tried this but it is not working...

<% foreach (var item in Model ) { %>
 <%: Html.HiddenFor(model => model.ID,new { @value="0"})%>
 <% } %>
tereško
  • 58,060
  • 25
  • 98
  • 150
coolguy97
  • 565
  • 1
  • 7
  • 12

2 Answers2

10

You can create your own helper extension for that:

public static MvcHtmlString HiddenFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes)
{
    var propertyName = ExpressionHelper.GetExpressionText(expression);

    var input = new TagBuilder("input");
    input.MergeAttribute("id", helper.AttributeEncode(helper.ViewData.TemplateInfo.GetFullHtmlFieldId(propertyName)));
    input.MergeAttribute("name", helper.AttributeEncode(helper.ViewData.TemplateInfo.GetFullHtmlFieldName(propertyName)));
    input.MergeAttribute("value", value.ToString());
    input.MergeAttribute("type", "hidden");
    input.MergeAttributes(new RouteValueDictionary(htmlAttributes));

    return MvcHtmlString.Create(input.ToString());
}
Necros
  • 3,004
  • 23
  • 29
2

Simply set the ID property of your model in the controller action to 0.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • K understood .but i need to do the processing on the ID property of my model means i am using it on other field also. – coolguy97 Aug 17 '10 at 13:01
  • In this case I am afraid you will have to generate the HTML manually without using the `HiddenFor` helper. Also why do you need a hidden field whose value is always set to `0`? Do you have some other javascript code that would later modify the value? – Darin Dimitrov Aug 17 '10 at 13:25
  • 1
    yes i have the javascript code which would change the hidden field value on check box check. – coolguy97 Aug 17 '10 at 13:34