與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);
}
所以,我現在有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)工作得很好,它procudes下面的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>
[/snip]
謝謝,但沒有去。 使用網格時的模型是Ienumerable。網格不允許爲每行提供單獨的邏輯。 我認爲我堅持我目前的情況.. –
你的意思是說,你正在使用的實際視圖有它的模型定義爲IEnumerable而不是IEnumerable (或任何你想要的實體)? –
沒有它的IEnumerable,但是因爲它的集合,你不能訪問單個Foo的屬性,直到選中一個。網格不會爲要測試的項目提供「行特定」屬性。對不起,如果這是令人困惑。 –