asp.net-mvc – Asp.Net MVC 2 Label自定义文本
发布时间:2020-05-28 00:04:03 所属栏目:asp.Net 来源:互联网
导读:有没有办法使用LabelFor帮助器并自定义标签文本,而不必在我的模型中使用DisplayNameAttribute? 我为我的项目创建了这个html助手: public static class MyLabelExtensions{ public static MvcHtmlString Label(this HtmlHelper htmlHelper, string forName,
|
有没有办法使用LabelFor帮助器并自定义标签文本,而不必在我的模型中使用DisplayNameAttribute? 解决方法我为我的项目创建了这个html助手:public static class MyLabelExtensions
{
public static MvcHtmlString Label(this HtmlHelper htmlHelper,string forName,string labelText)
{
return Label(htmlHelper,forName,labelText,(object) null);
}
public static MvcHtmlString Label(this HtmlHelper htmlHelper,string labelText,object htmlAttributes)
{
return Label(htmlHelper,new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString Label(this HtmlHelper htmlHelper,IDictionary<string,object> htmlAttributes)
{
var tagBuilder = new TagBuilder("label");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttribute("for",forName.Replace(".",tagBuilder.IdAttributeDotReplacement),true);
tagBuilder.SetInnerText(labelText);
return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
}
public static MvcHtmlString LabelFor<TModel,TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel,TProperty>> expression,string labelText)
{
return LabelFor(htmlHelper,expression,(object) null);
}
public static MvcHtmlString LabelFor<TModel,object htmlAttributes)
{
return LabelFor(htmlHelper,new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString LabelFor<TModel,object> htmlAttributes)
{
string inputName = ExpressionHelper.GetExpressionText(expression);
return htmlHelper.Label(inputName,htmlAttributes);
}
}
我用“强类型”资源使用它们: <%= Html.LabelFor(m=>m.NickName,UserStrings.NickName) %> 希望有帮助… (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC 5如何在Identity 2.0中删除用户
- asp.net-mvc – ASP.NET MVC区域可以显示自己的错误页面集吗
- IIS“SendResponse”状态下的请求已停留很长时间;慢IIS 7.5
- ASP.NET MVC Model绑定的简单应用
- asp.net – 当绑定到XmlDataSource时如何编程设置Dropdownl
- asp.net – IIS 7.5中的URL重写会导致内部服务器错误
- asp.net – WebForms中的Tabbing行为
- .net – Control.ResolveUrl与Control.ResolveClientUrl对比
- 使用AspNetPager与GridView完成分页
- asp.net-mvc-4 – ASP.NET Active Directory搜索
推荐文章
站长推荐
- asp.net – 使用NuGet发布可执行文件和网站
- asp.net – 授权web.config中的属性对授权节点
- asp.net-mvc – asp.net mvc – 如何告诉子操作忽
- asp.net-mvc-3 – 具有最佳实践的示例N层ASP.NET
- asp.net-mvc-3 – DotNetOpenAuth.Asp在MVC4应用
- 将变量从ASP.net传递给JavaScript
- asp.net-mvc-3 – 处理DbContext后的问题
- asp.net-mvc – 在asp .net mvc 5中显示外部网页
- asp.net-mvc – 从asp.net mvc控制器返回一个js文
- asp.net-mvc – 首先通过NuGet尝试StructureMap和
热点阅读
