2010-08-17 44 views

回答

10

您可以创建自己的助手扩展为:

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

在控制器动作只需设置你的模型的ID属性0

+0

ķ理解。但我需要做我的模型的ID属性的处理是指我使用它在其他领域也。 – coolguy97 2010-08-17 13:01:46

+0

在这种情况下,我担心你将不得不手动生成HTML,而不使用'HiddenFor'助手。另外为什么你需要一个隐藏的字段,其值始终设置为“0”?你有一些其他的JavaScript代码,以后会修改值吗? – 2010-08-17 13:25:00

+1

是的我有JavaScript代码,这将改变复选框检查隐藏的字段值。 – coolguy97 2010-08-17 13:34:24