C#Pass Class Property Name作为参数

时间:2015-07-18 12:33:50

标签: c# asp.net-mvc razor reflection html-helper

在我的剃刀视图中,我使用HTML Helper Extension方法,以便使用Model Property的Display Name属性在文本框中显示占位符值。见下面的例子。

我的助手扩展方法:

    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属性)。 我怎样才能使模型数据注释属性可以在剃刀视图中传递,占位符使用该属性的值呢?

我想要的示例代码如下。

传递了带有元数据属性的剃刀视图:

@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);
    }

1 个答案:

答案 0 :(得分:0)

您可以使用以下

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;
}