我有一个名为Lookup的实体,它有一个复杂的Type Description,带有两个英文和法文字符串。有一段时间没有值存储到查找中。现在其他实体将查找作为属性,因此我们可以使用foo.Lookup.Description.English。
我正在尝试使用网格来显示所选的信息。
最初我的控制器看起来像
public ViewResult Index()
{
var foos = db.Foos;
return View(foo.ToList());
}
我的观点看起来像
@model IEnumerable<Foo>
@{
ViewBag.Title = "Index";
}
<h2>Stay Manager</h2>
@{
var grid = new WebGrid(Model, defaultSort: "sortMe", rowsPerPage: 3);
grid.GetHtml(htmlAttributes: new { id = "DataTable" });
}
@grid.GetHtml(columns: grid.Columns(
grid.Column("Lookup.Description.English", "Column Header")
))
我的问题是,Lookup有时可能为null并且生病会收到错误,指出Column Lookup.Description.English不存在。
我找到了一个解决方案,但不是一个非常优雅的解决方案,并希望有更好的方法。我的解决方案是将我的控制器操作更改为
public ViewResult Index()
{
var foos = db.Foos;
foreach (Foo currentFoo in Foos.Where(s => s.Lookup == null))
{
Foo.Lookup = new Lookup();
Foo.Lookup.Description.English = "";
Foo.Lookup.Description.French = "";
}
return View(foos.ToList());
}
有关如何使用无效复杂类型使Web Grid更好地工作的任何建议吗?
答案 0 :(得分:4)
与webgrid完全不同,但以下是否适合您? 我做了以下简单的模型:
public class Foo
{
public string Name { get; set; }
public Lookup Lookup { get; set; }
}
public class Lookup
{
public string Name { get; set; }
public Description Description { get; set; }
}
public class Description
{
public string English { get; set; }
public string French { get; set; }
}
控制器动作(我没有数据库,所以我嘲笑了一些数据):
public ViewResult Index()
{
//var foos = db.Foos;
var foos = new List<Foo>();
foos.Add(new Foo { Name = "Foo1" });
foos.Add(new Foo
{
Name = "Foo2",
Lookup = new Lookup
{
Name = "Lookup2",
Description = new Description
{
English = "englishFoo2",
French = "frenchFoo2"
}
}
});
foos.Add(new Foo
{
Name = "Foo3",
Lookup = new Lookup
{
Name = "Lookup3",
Description = new Description
{
//English = "englishFoo3",
French = "frenchFoo3"
}
}
});
foos.Add(new Foo { Name = "Foo4" });
foos.Add(new Foo
{
Name = "Foo5",
Lookup = new Lookup
{
Description = new Description
{
English = "englishFoo5",
French = "frenchFoo5"
}
}
});
foos.Add(new Foo {
Name = "Foo6",
Lookup = new Lookup
{
Name = "Lookup6"
}
});
return View(foos);
}
所以,我现在有了或不带Lookup的Foos(带或不带说明)。
观点如下:
@model IEnumerable<Foo>
@{
var grid = new WebGrid(Model, defaultSort: "sortMe", rowsPerPage: 10);
grid.GetHtml(htmlAttributes: new { id = "DataTable" });
}
@grid.GetHtml(
columns: grid.Columns(
grid.Column(
columnName: "Name",
header: "Foo"
),
grid.Column(
columnName: "Lookup.Name",
header: "Lookup",
format: @<span>@if (item.Lookup != null)
{ @item.Lookup.Name }
</span>
),
grid.Column(
columnName: "Lookup.Description.English",
header: "Description.English",
format: @<span>@if (item.Lookup != null && item.Lookup.Description != null)
{ @item.Lookup.Description.English }
</span>
),
grid.Column(
columnName: "Lookup.Description.French",
header: "Description.French",
format: @<span>@if (item.Lookup != null && item.Lookup.Description != null)
{ @item.Lookup.Description.French }
</span>
)
)
)
对我来说效果很好(Asp.Net MVC 3),它会产生以下html:
[剪断]
<tr>
<td>Foo4</td>
<td><span></span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
<tr>
<td>Foo5</td>
<td><span></span></td>
<td><span>englishFoo5 </span></td>
<td><span>frenchFoo5 </span></td>
</tr>
<tr>
<td>Foo6</td>
<td><span>Lookup6</span></td>
<td><span></span></td>
<td><span></span></td>
</tr>
[/剪断]