HTML我希望能够做这样的事情:ASP MVC强类型的无剃刀
<input [email protected] (as the actual property name and not its value) [email protected] type="text/>
使模型绑定仍然工作,但我并不需要使用剃刀帮手。
HTML我希望能够做这样的事情:ASP MVC强类型的无剃刀
<input [email protected] (as the actual property name and not its value) [email protected] type="text/>
使模型绑定仍然工作,但我并不需要使用剃刀帮手。
您可以添加一个辅助方法返回的显示名称:
public static MvcHtmlString GetPropertyName<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
var metaData = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
string value = metaData.PropertyName ?? expressionHelper.GetExpressionText(expression);
return MvcHtmlString.Create(value);
}
然后你只需使用:@ Html.GetPropertyName(M => m.SomeProperty)
你不应该编辑:在回答将是:使用HtmlHelpers(他们将是正确的!) –
@RaphaëlAlthaus我想到了这一点,并意识到它们在某些情况下确实有价值,但该死的他们可能会对简单的事情感到恼火!,所以我重新说明了我的问题。 – Lee