c# - 使用泛型和 LINQ 的 MVC3 HtmlHelpers

标签 c# linq asp.net-mvc-3 html-helper

已更新

我正在尝试创建一个能够获取 TModel 集合的 HtmlHelper 扩展。然后设置一个表达式,它将获取集合中每个项目的声明属性并将它们打印出来。

下面的这段代码确实有效。但是,在我目前拥有的模式中,我不喜欢的一件事是无法推断泛型类型。我必须为我的扩展方法声明类型。

我正在尝试遵循 Telerik 用于其 Grid 的模式类型,当您在其中声明模型时,将推断出通用类型。

我想这样做:

@model List<MyProject.MyModels.UserModel>
@(Html.MyExtensions()
      .PrintUsers(Model)
      .FirstName(m => m.FirstName)
      .LastName(m => m.LastName)
)

根据我目前的模式,我必须这样做:

@model List<MyProject.MyModels.UserModel>
@(Html.MyExtensions<List<MyProject.MyModels.UserModel>, MyProject.MyModels.UserModel>()
      .PrintUsers(Model)
      .FirstName(m => m.FirstName)
      .LastName(m => m.LastName)
)

所以我需要找出一个更好的模式来推断我的类型。 有什么想法吗?

UserModel 看起来像:

public class UserModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

所以我有一个看起来像这样的 HtmlHelper:

public static class UIExtension
{
    public static ComponentFactory<TModel, T> MyExtensions<TModel, T>(this HtmlHelper<TModel> html) 
        where TModel : IEnumerable<T>
        where T : class
    {
        return new ComponentFactory<TModel, T>(html);
    }
}

因此,我的一个组件将获取一个列表,然后通过将每个已声明的属性从 Linq 表达式打印到页面进行迭代。

在我的 ComponentFactory 中我有这个:

public class ComponentFactory<TModel, T>
    where T : class
{
    private HtmlHelper<TModel> _html;

    public ComponentFactory()
    {
    }

    public ComponentFactory(HtmlHelper<TModel> html)
    {
        this._html = html;
    }

    public ListComponent<T> PrintUsers(IEnumerable<T> model)
    {
        return new ListComponent<T>(model);
    }

    /* Add other ui components to this factory */
}

然后是我的 ListComponent

public class ListComponent<T> : IHtmlString
{
    private Expression<Func<T, string>> _firstname;
    private Expression<Func<T, string>> _lastname;

    private IEnumerable<T> _model;

    public ListComponent(IEnumerable<T> model)
    {
        this._model = model;
    }

    public ListComponent<T> FirstName(Expression<Func<T, string>> property)
    {
        this._firstname = property;
        return this;
    }

    public ListComponent<T> LastName(Expression<Func<T, string>> property)
    {
        this._lastname = property;
        return this;
    }

    public override MvcHtmlString Render()
    {
        TagBuilder container = new TagBuilder("div");

        TagBuilder title = new TagBuilder("div");
        title.InnerHtml = "<b>Names</b>";
        container.InnerHtml = title.ToString(TagRenderMode.Normal);

        TagBuilder ul = new TagBuilder("ul");

        foreach (var item in this._model)
        {
            TagBuilder li = new TagBuilder("li");
            li.SetInnerText(String.Format("{0} {1}", _firstname.Compile()(item), _lastname.Compile()(item)));
            ul.InnerHtml += li.ToString(TagRenderMode.Normal);
        }

        container.InnerHtml += ul.ToString(TagRenderMode.Normal);

        return MvcHtmlString.Create(container.ToString(TagRenderMode.Normal));
    }
}

感谢您的帮助和建议!

最佳答案

像这样的事情怎么样:

public static class UIExtension
{
    public static ComponentFactory<TModel> MyExtensions<TModel>(this HtmlHelper<IEnumerable<TModel>> html) where TModel : class

    {
        return new ComponentFactory<TModel>(html);
    }
}
public class UserModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
public class ComponentFactory<TModel>
where TModel : class
{
    private HtmlHelper _html;

    public ComponentFactory()
    {
    }

    public ComponentFactory(HtmlHelper<IEnumerable<TModel>> html)
    {
        this._html = html;
    }

    public ListComponent<TModel> PrintUsers(IEnumerable<TModel> model)
    {
        return new ListComponent<TModel>(model);
    }

    /* Add other ui components to this factory */
}
public class ListComponent<T> : IHtmlString
{
    private Expression<Func<T, string>> _firstname;
    private Expression<Func<T, string>> _lastname;

    private IEnumerable<T> _model;

    public ListComponent(IEnumerable<T> model)
    {
        this._model = model;
    }

    public ListComponent<T> FirstName(Expression<Func<T, string>> property)
    {
        this._firstname = property;
        return this;
    }

    public ListComponent<T> LastName(Expression<Func<T, string>> property)
    {
        this._lastname = property;
        return this;
    }



public override MvcHtmlString Render()
{
    TagBuilder container = new TagBuilder("div");

    TagBuilder title = new TagBuilder("div");
    title.InnerHtml = "<b>Names</b>";
    container.InnerHtml = title.ToString(TagRenderMode.Normal);

    TagBuilder ul = new TagBuilder("ul");

    foreach (var item in this._model)
    {
        TagBuilder li = new TagBuilder("li");
        li.SetInnerText(String.Format("{0} {1}", _firstname.Compile()(item), _lastname.Compile()(item)));
        ul.InnerHtml += li.ToString(TagRenderMode.Normal);
    }

    container.InnerHtml += ul.ToString(TagRenderMode.Normal);

    return MvcHtmlString.Create(container.ToString(TagRenderMode.Normal));
}
}

那么用法如下:

Html.MyExtensions().PrintUsers(Model).FirstName(p=>p.FirstName).LastName(p=>p.LastName)

关于c# - 使用泛型和 LINQ 的 MVC3 HtmlHelpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6524180/

相关文章:

asp.net-mvc-3 - 在 MVC3 中检查上传文件是否有病毒

c# - 使用 Json.Net 解析 forecast.io 天气数据时遇到问题

c# - 从 Windows 窗体键中剥离修饰符

c# - UWP 将文件保存在文档和图片库中

c# - 重写虚方法时,调用基方法是最佳实践吗?

c# - `Aggregate`怎么写成 `from .. in ..`?

asp.net-mvc-3 - MVC 中的 SSO(单点登录)

LINQ OrderBy 对内部对象的属性

c# - InvalidOperationException : type 'System.String' referenced from scope '' , 但未定义

asp.net-mvc - ASP.Net MVC 3 : Child Action and Redirect