gpt4 book ai didi

C# 将类属性名称作为参数传递

转载 作者:太空宇宙 更新时间:2023-11-03 15:37:06 25 4
gpt4 key购买 nike

在我的 razor View 中,我使用 HTML 帮助扩展方法,以便使用我的模型属性的显示名称属性在文本框中显示占位符值。请参见下面的示例。

我的助手扩展方法:

    public static MvcHtmlString TextBoxPlaceHolderFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();

if (!String.IsNullOrEmpty(labelText))
{
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
htmlAttributes.Add("placeholder", labelText);
}
return html.TextBoxFor(expression, htmlAttributes);
}

我的模型属性:

    [Display(Name = "First Name", Description = "Enter Display Name")]
public string FirstName { get; set; }

当前结果:

属性的显示名称在字段内显示为占位符。

但是,有时我可能想在模型上使用另一个属性作为占位符文本(如上面模型中的 Description 属性)。我怎样才能做到这一点,以便可以在 Razor View 中传递模型数据注释属性,而占位符改为使用该属性的值?

下面是我想要的示例代码。

传递了元数据属性的 Razor View :

@Html.TextBoxPlaceHolderFor(model => model.FirstName, new { @class = "form-control" }, MODELMETADATAPROPERTY HERE)

HTML Helper Extension(我想象代码会做什么,可能需要使用反射或其他东西?)

 public static MvcHtmlString TextBoxPlaceHolderFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes, MODELMETADATAPROPERTY HERE)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.MODELMETADATAPROPERTY HERE;

if (!String.IsNullOrEmpty(labelText))
{
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
htmlAttributes.Add("placeholder", labelText);
}
return html.TextBoxFor(expression, htmlAttributes);
}

最佳答案

您可以使用以下内容

public static MvcHtmlString TextBoxPlaceHolderFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.DisplayName ?? Proper(metadata.PropertyName?? htmlFieldName.Split('.').Last());

if (!String.IsNullOrEmpty(labelText))
{
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
htmlAttributes.Add("placeholder", modeldata.Description??Proper(metadata.PropertyName?? htmlFieldName.Split('.').Last())); // to get the description value
}
return html.TextBoxFor(expression, labelText);
}

// this function will convert FullName to Full Name with space
private static string Proper(string value)
{
var newValue = Regex.Replace(value, "([a-z])([A-Z])", "$1 $2");
return newValue;
}

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

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