使用泛型和 LINQ 的 MVC3 HtmlHelpers

发布于 2024-11-17 21:20:32 字数 3368 浏览 2 评论 0原文

更新

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

下面的代码确实有效。然而,我在当前模式中不喜欢的一件事是没有推断出泛型类型。我必须声明我的扩展方法的类型。

我试图遵循 Telerik 用于其网格的模式类型,当您声明模型时,就会推断通用类型。

在我看来,我想这样做:

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

感谢您的帮助和建议!

UPDATED

I am trying to create an HtmlHelper Extension that has the ability to take a collection of TModels. Then set an Expression that will grab the declared property foreach item in the collection and print them out.

This code here below does work. However, the one thing I don't like in the pattern I currently have is the generic types are not inferred. I have to declare the types for my extension method.

I am trying to follow the type of pattern that Telerik used for their Grid, where when you declare a model, the generic types are then inferred.

I would like to do this in my view:

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

With my current pattern I have to do this:

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

So I need to figure out a better pattern so my types are inferred.
Any ideas?

UserModel looks like:

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

So I have an HtmlHelper that looks like this:

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

So one of my components would take a List and then iterate thru printing out each declared property from a Linq Expression to the page.

In my ComponentFactory I have this:

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 */
}

Then my 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));
    }
}

Thanks for any help and suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开雨落又逢春i 2024-11-24 21:20:32

怎么样:

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)

What about something like this:

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

The usage is then as follows:

Html.MyExtensions().PrintUsers(Model).FirstName(p=>p.FirstName).LastName(p=>p.LastName)
一梦等七年七年为一梦 2024-11-24 21:20:32

看了@Tomas 的答案后,它帮助我意识到我在模式中遗漏了什么。虽然 @Tomas 的回答确实让我在正确推断了 ModelView 中得到了我想要的语法,但唯一的缺点是整个 ViewModel 与 IEnumerable 绑定。由于我想绑定到我的 ViewModel 中的 IEnumerable 属性,我做了一些更改,结果成功了。

HtmlHelper 扩展:

public static class UIExtension
{
   public static ComponentFactory MyExtensions(this HtmlHelper html)
   {
       return new ComponentFactory(html);
   }

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

ComponentFactory

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

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

因此,主要的更改是对于 PrintUsers 方法,我使用不同的通用类型来处理该部分。这样,我的 Model 就可以是开发人员决定的任何内容,他们只需将 Property 设置为用作 ListComponent< 的 datasource /代码>。

所以现在在 View 中它非常灵活,我可以做这样的事情......

@(Html.MyExtensions()
      .PrintUsers(Model.MyList)
      .FirstName(m => m.FirstName)
      .LastName(m => m.LastName)
)

After taking a look at @Tomas' answer, it helped me realize what I was missing in the pattern. Although @Tomas' answer did get me the syntax I wanted in the View where the Model was being inferred correctly, the only draw back was the entire ViewModel was tehn tied to an IEnumerable<T>. Since I would like to bind to a property in my ViewModel that is of IEnumerable I made a couple changes and it worked out.

The HtmlHelper Extension:

public static class UIExtension
{
   public static ComponentFactory MyExtensions(this HtmlHelper html)
   {
       return new ComponentFactory(html);
   }

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

The ComponentFactory:

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

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

So the main change is that for the PrintUsers method, I use a different Generic Type to handle that portion. This way my Model can be what ever the developer decides and they simply set the Property to use as the datasource for the ListComponent.

So now in the View it's very flexible, I can do something like this...

@(Html.MyExtensions()
      .PrintUsers(Model.MyList)
      .FirstName(m => m.FirstName)
      .LastName(m => m.LastName)
)
画▽骨i 2024-11-24 21:20:32

您可以使用两种类型参数,一种用于列表,一种用于项目类型:

ListComponent<TList, TItem> Print<TList, TItem>(TList list) where TList : IEnumerable<TItem>

更新:
也许这样类型推断就会起作用:

ListComponent<TItem> Print<TItem>(IEnumerable<TItem> list)

You could use two type parameters, one for the list and one for the item type:

ListComponent<TList, TItem> Print<TList, TItem>(TList list) where TList : IEnumerable<TItem>

UPDATE:
Maybe this way the type inference will work:

ListComponent<TItem> Print<TItem>(IEnumerable<TItem> list)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文