MVC HTML Helper:来自字符串表达式的modelmetadata [英] MVC Html Helper: modelmetadata from string expression

查看:120
本文介绍了MVC HTML Helper:来自字符串表达式的modelmetadata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个可以访问modelmetadata的html帮助器. 我需要两种版本的辅助程序才能工作:从字符串表达式和从lambda表达式: 示例:

I am trying to build an html helper that would have access to modelmetadata. I need both versions of helper to work: from string expression and from lambda expression: Example:

public static MvcHtmlString MyLabel(this HtmlHelper html, string htmlFieldName)
{
    return LabelHelper(html, htmlFieldName);
}

public static MvcHtmlString MyLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
    return LabelHelper(html, ExpressionHelper.GetExpressionText(expression));
}

private MvcHtmlString LabelHelper(HtmlHelper html, string htmlFieldName)
{
     ModelMetadata m = ModelMetadata.FromStringExpression(htmlFieldName);
     // the rest of the code...
}

上面的代码的问题是它不适用于复杂类型.例如,如果我的模型看起来像这样:

The problem with the code above is that it will not work for complex types. For example, if my Model looked like this:

public class MyViewModel
{
    public int Id { get; set; }
    public Company Company { get; set; }
}

public class Company
{
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
}

我的html帮助器将无法读取以下内容的元数据:

My html helper will fail to read metadata for the following:

@Html.MyLabel("Company.Name")

我可以使它对带有表达式的帮助程序起作用,因为ModelMetadata.FromLambdaExpression(...)实际上可以与复杂的对象配合使用,但这对我来说还不够.

I could make it work for the helper that takes an expression because ModelMetadata.FromLambdaExpression(...) actually works fine with complex objects, but that is not enough for me.

任何建议都值得赞赏.

推荐答案

总之,将无法仅使用FromStrinExpression(...)方法.在内部,ModelMetadata.FromStringExpression(...)将尝试获取嵌套属性-名称"的ViewDataInfo.如果View是严格类型的,但是Model为null,则
ViewData.GetViewDataInfo将返回null.在这种情况下,它将仅循环ModelMetadata.Properties,并且将无法找到嵌套的属性.如果Model不为null,则由于正确的ViewDataInfo,该方法将返回正确的ModelMetadata.另一方面,ModelMetadata.FromLamdaExpression(...)拥有有关容器和属性类型的足够信息,这就是为什么它可用于复杂对象的原因.

In a word, it will not be possible to use only the FromStrinExpression(...) method. Internally the ModelMetadata.FromStringExpression(...) will try to get the ViewDataInfo for the nested property - "Name" in your case. If the View is a stongly-typed, but the Model is null then the
ViewData.GetViewDataInfo will return null. In this case it will loop only the ModelMetadata.Properties and will not be able to find the nested property. If the Model is not null, then the method will return the correct ModelMetadata, because of the correct ViewDataInfo. The ModelMetadata.FromLamdaExpression(...) on the other has enough information about the container and the type of the property and that is why it works with complex objects.

我有一个勇敢的建议:).您具有字符串表达式和Html.ViewData.您可以递归循环Html.ViewData.ModelMetadata.Properties并尝试获取嵌套属性的ModelMetadata.

I have one brave suggestion :). You have the string expression and the Html.ViewData. You can loop the Html.ViewData.ModelMetadata.Properties recursively and try to get the ModelMetadata for the nested property.

这篇关于MVC HTML Helper:来自字符串表达式的modelmetadata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆