asp.net-mvc-2 – 如何在Asp.Net MVC中为HiddenFieldFor设置默认
发布时间:2020-12-16 07:31:18 所属栏目:asp.Net 来源:网络整理
导读:我正在使用HiddenFor与模型绑定,它绑定值. 我想将绑定值重置为零.我该怎么做? 我试过这个,但它不起作用…… % foreach (var item in Model ) { % %: Html.HiddenFor(model = model.ID,new { @value="0"})% % } % 解决方法 您可以为此创建自己的帮助程序扩展
我正在使用HiddenFor与模型绑定,它绑定值.
我想将绑定值重置为零.我该怎么做? 我试过这个,但它不起作用…… <% foreach (var item in Model ) { %> <%: Html.HiddenFor(model => model.ID,new { @value="0"})%> <% } %> 解决方法
您可以为此创建自己的帮助程序扩展:
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()); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 是否有任何ASP.NET应用程序来监控在线用户和页面查看日志报
- asp.net-mvc – MVC2中的REQUIRED String属性的服务器端验证
- asp.net-core-mvc – “:exists”在路由模板上做了什么?
- 我们可以通过.net native编译asp.net 5应用程序吗?
- asp.net-mvc – 将HTML属性添加到Html.BeginForm()的变体
- asp.net-mvc-4 – MVC4中的Bootstrap和font-awesome
- ASP.NET MVC null模型传递给控制器??动作
- vbscript – 尝试在.VBS文件中“包含”.ASP文件
- asp.net-mvc-3 – 如何不使用Ninject的内核作为资源定位器
- asp.net-mvc-4 – 如何使用业务标识提供程序(例如ADFS2)
推荐文章
站长推荐
- asp.net – Windows 8 RTM上的Visual Studio 201
- asp.net – 优化的捆绑包在从网站请求时返回404
- asp.net-mvc-3 – ASP.NET MVC 3认证/授权
- asp.net – System.Web.Security.MembershipProv
- asp.net-mvc – ASP.NET MVC:什么在哪里?
- asp.net-core – 使用ASP.NET Core从`project.js
- dependency-injection – 如何使用ASP.NET MVC 3
- asp.net – 在序列化“System.Reflection”类型的
- asp.net-mvc – ASP.NET MVC如何使用ASP.NET成员
- asp.net-mvc – 确保视图存在
热点阅读