开发者

How can i set Default value for HiddenFieldFor In Asp.Net MVC

i am using HiddenFor with model binding which is binding value开发者_JAVA技巧 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"})%>
 <% } %>


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());
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜