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