我想为html.labelfor添加一个扩展名,以便我可以使用类似的东西:
@Html.LabelFor(m=>m.Description,":")
哪个会在呈现的HTML中添加分号作为后缀:
Description:
解决方法
你可以这样做:
@Html.LabelFor(m => m.Description,string.Format("{0}:",Html.DisplayNameFor(m => m.Description)))
DisplayNameFor只为您提供没有标签标记的属性的显示名称,因此您可以将其用作LabelFor中labelText参数的一部分,并根据需要对其进行格式化.这样您仍然可以获得动态生成的标签文本的好处.
轻松地包裹在自己的帮手中:
public static MvcHtmlString LabelWithColonFor<TModel,TValue>( this HtmlHelper<TModel> helper,Expression<Func<TModel,TValue>> expression) { return helper.LabelFor(expression,helper.DisplayNameFor(expression))); }
然后:
@Html.LabelWithColonFor(m => m.Description)