我有一个解决方法来创建我的自定义 @Html.LabelFor() 和 @html.DescriptionFor() 助手。
我的帮手:
namespace MyCMS.Helpers
{
public static class Html
{
public static MvcHtmlString DescriptionFor<TModel, TValue>(
this HtmlHelper<TModel> self,
Expression<Func<TModel, TValue>> expression)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
var description = Localizer.Translate(metadata.Description);
return MvcHtmlString.Create(string.Format(@"<span class=""help-block"">{0}</span>", description));
}
public static MvcHtmlString LabelFor<TModel, TValue>(
this HtmlHelper<TModel> self,
Expression<Func<TModel, TValue>> expression,
bool showToolTip
)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, self.ViewData);
var name = Localizer.Translate(metadata.DisplayName);
return MvcHtmlString.Create(string.Format(@"<label for=""{0}"">{1}</label>", metadata.PropertyName, name));
}
}
}
我的看法是:
@使用 MyCMS.Localization; @使用 MyCMS.Helpers;
<div class="clearfix ">
@Html.LabelFor(model => model.RecordDetails.TitleAlternative)
<div class="input">
@Html.TextBoxFor(model => model.RecordDetails.TitleAlternative, new { @class = "xxlarge" })
@Html.ValidationMessageFor(model => model.RecordDetails.TitleAlternative)
@Html.DescriptionFor(model => model.RecordDetails.TitleAlternative)
</div>
</div>
我可以使用我的本地化方法:)
再次感谢大家...