gpt4 book ai didi

c# - 将 displayname 属性作为参数传递

转载 作者:太空狗 更新时间:2023-10-30 00:33:42 27 4
gpt4 key购买 nike

对于以下 ActionLink 调用:

@Html.ActionLink("Customer Number", "Search", new { Search = ViewBag.Search, q = ViewBag.q, sortOrder = ViewBag.CustomerNoSortParm, })

我正在尝试传递@model.CustomerNumber 的标签以生成“客户编号”文本,而不必显式传递它。参数是否有 @Html.LabelFor(model => model.CustomerNumber) 的等价物?

最佳答案

没有开箱即用的助手。

但是编写自定义的非常容易:

public static class HtmlExtensions
{
public static string DisplayNameFor<TModel, TProperty>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TProperty>> expression
)
{
var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
return (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new[] { '.' }).Last()));
}
}

然后使用它(在将您定义它的 namespace 引入范围之后):

@Html.ActionLink(
"Customer Number",
"Search",
new {
Search = ViewBag.Search,
q = ViewBag.q,
sortOrder = ViewBag.CustomerNoSortParm,
customerNumberDescription = Html.DisplayNameFor(model => model.CustomerNumber)
}
)

关于c# - 将 displayname 属性作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10602061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com