我怎么能在我的HtmlHelper调用`EditorExtensions.EditorFor`? [英] How can I invoke `EditorExtensions.EditorFor` in my HtmlHelper?

查看:123
本文介绍了我怎么能在我的HtmlHelper调用`EditorExtensions.EditorFor`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CreateView的使用不同的模型,都继承BaseModel。要调用EditorFor我创建了一个的HtmlHelper,获取模型和实际财产的权利。但我不知道如何调用它。

BaseModel:

 公共抽象类BaseModel
{
    保护的IEnumerable<&的PropertyInfo GT; PropertyInfoCache {搞定;组; }
    保护的IEnumerable< EnumeratedProperty> EnumeratedPropertyCache {搞定;组; }
    保护BaseModel()
    {
        PropertyInfoCache = this.GetType()的GetProperties()。
        EnumeratedPropertyCache = PropertyInfoCache.Select(P =>新建EnumeratedProperty(p.Name,p.GetType()));
    }
    公共IEnumerable的< EnumeratedProperty> EnumerateProperties()
    {
        返回EnumeratedPropertyCache;
    }
    公共对象GetPropertyValue(字符串属性名)
    {
        VAR财产= PropertyInfoCache.SingleOrDefault(I => i.Name ==属性名);
        如果(财产!= NULL)
            返回property.GetValue(本,NULL);
        返回null;
    }
}公共类EnumeratedProperty
{
    公共字符串名称{;私人集; }
    公共类型类型{搞定;私人集; }
    公共EnumeratedProperty(字符串属性名,类型属性类型)
    {
        this.Name =属性名;
        this.Type =属性类型;
    }
}

在我看来:

  @foreach(在Model.EnumerateProperties VAR财产())
{
    @ Html.EditorForProperty(型号,特性);
}

的HtmlHelper:

 公共静态类ExtensionMethods
{
    公共静态MvcHtmlString EditorForProperty(这个HTML的HtmlHelper,BaseModel型号,EnumeratedProperty属性)
    {
        //创建一个错误:对法'EditorFor'的类型参数不能从使用推断。请尝试显式指定类型参数。
        返回System.Web.Mvc.Html.EditorExtensions.EditorFor(HTML,型号= GT; Model.GetPropertyValue(property.Name));
    }
}


解决方案

EditorFor识别对象的类型,所以你会想要做的是从EnumeratedProperty类的价值提取,而不是直接传递类的类型, ,并在值传递从它。

I use different Models in my CreateView, all inherit from BaseModel. To call the right EditorFor I have created a HtmlHelper that gets the Model and the actual property. But I don´t know how to invoke it.

BaseModel:

public abstract class BaseModel
{
    protected IEnumerable<PropertyInfo> PropertyInfoCache { get; set; }
    protected IEnumerable<EnumeratedProperty> EnumeratedPropertyCache { get; set; }
    protected BaseModel()
    {
        PropertyInfoCache = this.GetType().GetProperties();
        EnumeratedPropertyCache = PropertyInfoCache.Select(p=> new EnumeratedProperty(p.Name,p.GetType()));
    }
    public IEnumerable<EnumeratedProperty> EnumerateProperties()
    {
        return EnumeratedPropertyCache;
    }
    public object GetPropertyValue(string PropertyName)
    {
        var property = PropertyInfoCache.SingleOrDefault(i=>i.Name==PropertyName);
        if(property!=null)
            return property.GetValue(this,null);
        return null;
    }
}

public class EnumeratedProperty
{
    public string Name { get; private set; }
    public Type Type { get; private set; }
    public EnumeratedProperty(string PropertyName, Type PropertyType)
    {
        this.Name = PropertyName;
        this.Type = PropertyType;
    }
}

in my View:

@foreach (var property in Model.EnumerateProperties())
{
    @Html.EditorForProperty(Model,property);
}

HtmlHelper:

public static class ExtensionMethods
{
    public static MvcHtmlString EditorForProperty(this HtmlHelper html, BaseModel Model, EnumeratedProperty property)
    {
        // creates an error: The type arguments for method 'EditorFor' cannot be inferred from the usage. Try specifying the type arguments explicitly.
        return System.Web.Mvc.Html.EditorExtensions.EditorFor(html, Model => Model.GetPropertyValue(property.Name) );
    }
}

解决方案

EditorFor recognizes the type of an object, so what you would want to do is extract the type from the value in the EnumeratedProperty class, instead of passing the class directly, and pass in the value from it too.

这篇关于我怎么能在我的HtmlHelper调用`EditorExtensions.EditorFor`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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