【发布时间】:2012-01-11 14:48:06
【问题描述】:
上下文: 由于我们是在 C# MVC3 中开发的,因此我们希望设计一些类来处理网页上的表格。 (分页/搜索/等...)。
所以我们最终发现最好有以下类:
将包含所有其他对象并知道当前页面/当前搜索等的表对象...(其他信息)
public class Table<T> where T : IPrivateObject
{
...
public ICollection<Column<T>> Columns { get; set; }
public ICollection<Row<T>> Rows { get; set; }
public ICollection<RowMenu<T>> Menus { get; set; }
public ICollection<T> Items { get; set; }
public Table(
ICollection<T> inputItems,
ICollection<Column<T>> columns,
ICollection<RowMenuItem<T>> rowMenuItems,
...)
{
...
this.Columns = columns;
}
知道应该显示哪个属性和标题值的列对象
public class Column<T> where T : IPrivateObject
{
public string Value { get; set; }
public Expression<Func<T, object>> Property { get; set; }
public Column(Expression<Func<T, object>> property, string value)
{
this.Property = property;
this.Value = value;
}
}
其他课程不是很有趣,所以我不会在这里发布。
在控制器中,我们像这样使用这些类:
public ActionResult Index(string search = null, string sort = null, int order = 1, int take = 10, int page = 1)
{
ICollection<Person> people = prismaManager.PersonManager.Search(search);
ICollection<Column<Person>> columns= new List<Column<Person>>();
columns.Add(new Column<Person>(Person => Person, "Person"));
columns.Add(new Column<Person>(Person => Person.LastMembershipApproval, "Last Membership approval"));
Table<Person> table = people.ToTable(columns);
}
我们现在正在编写一个可以正确显示表格的助手。 它适用于标题,但是当我们想要使用 @Html.DisplayFor() 帮助器时,我们会遇到表达式问题。
这是我们目前拥有的内容:
private static string TableRows<T>(HtmlHelper<Table<T>> helper, Table<T> table) where T : IPrivateObject
{
StringBuilder sb = new StringBuilder();
foreach (var item in table.Items)
{
sb.AppendLine("<tr>");
foreach (var column in table.Columns)
{
sb.AppendLine("<td>");
sb.AppendLine(helper.DisplayFor(obj => ??? ).ToString()); // How should I use the Expression that is stored in the column but for the current element ?
sb.AppendLine("</td>");
}
sb.AppendLine("</tr>");
}
return sb.ToString();
}
为此,我们应该将存储在列中的表达式中的“Person”参数的值设置为当前项。
new Column<Person>(Person => Person, "Person"));
我们应该怎么做呢? 我们应该(如果可能的话)修改表达式来设置值吗? 我们是否应该使用旧表达式作为基本表达式重新创建一个新表达式?
我已经搜索了 3 天,但找不到任何答案。
感谢您的帮助。
更新:
问题是(正如@Groo 和@Darin Dimitrov 所说)Helper 的类型是 HtmlHelper> 而不是 HtmlHelper。 知道如何从 HtmlHelper> 中获取 HtmlHelper 吗?
更新:
Person类如下:
public class Person : IPrivateObject
{
public int Id { get; set; }
public int? AddrId { get; set; }
[DisplayName("First Name")]
[StringLength(100)]
[Required]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[StringLength(100)]
[Required]
public string LastName { get; set; }
[DisplayName("Initials")]
[StringLength(6)]
public string Initials { get; set; }
[DisplayName("Last membership approval")]
public Nullable<DateTime> LastMembershipApproval { get; set; }
[DisplayName("Full name")]
public string FullName
{
get
{
return FirstName + " " + LastName;
}
}
public override string ToString()
{
return FullName;
}
}
【问题讨论】:
标签: c# asp.net-mvc-3 lambda expression