使用泛型和 LINQ 的 MVC3 HtmlHelpers
更新
我正在尝试创建一个能够获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
用法如下:
What about something like this:
The usage is then as follows:
看了@Tomas 的答案后,它帮助我意识到我在模式中遗漏了什么。虽然 @Tomas 的回答确实让我在正确推断了
Model
的View
中得到了我想要的语法,但唯一的缺点是整个ViewModel 与
IEnumerable
绑定。由于我想绑定到我的ViewModel
中的 IEnumerable 属性,我做了一些更改,结果成功了。HtmlHelper
扩展:ComponentFactory
:因此,主要的更改是对于
PrintUsers
方法,我使用不同的通用类型来处理该部分。这样,我的Model
就可以是开发人员决定的任何内容,他们只需将Property
设置为用作ListComponent< 的
datasource
/代码>。所以现在在
View
中它非常灵活,我可以做这样的事情......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 theModel
was being inferred correctly, the only draw back was the entireViewModel
was tehn tied to anIEnumerable<T>
. Since I would like to bind to a property in myViewModel
that is of IEnumerable I made a couple changes and it worked out.The
HtmlHelper
Extension:The
ComponentFactory
:So the main change is that for the
PrintUsers
method, I use a different Generic Type to handle that portion. This way myModel
can be what ever the developer decides and they simply set theProperty
to use as thedatasource
for theListComponent
.So now in the
View
it's very flexible, I can do something like this...您可以使用两种类型参数,一种用于列表,一种用于项目类型:
更新:
也许这样类型推断就会起作用:
You could use two type parameters, one for the list and one for the item type:
UPDATE:
Maybe this way the type inference will work: