我有以下ViewModel:
public class ShowMatrixQuestionViewModel : ShowQuestionViewModel
{
public Dictionary<MatrixRows, List<MatrixColumns>> columnrow;
public List<MatrixColumns> columns;
public List<MatrixRows> rows;
public ShowMatrixQuestionViewModel()
{
columns = new List<MatrixColumns>();
rows = new List<MatrixRows>();
columnrow = new Dictionary<MatrixRows, List<MatrixColumns>>();
}
}
public class MatrixColumns
{
public int Column_ID { get; set; }
public int Column_Number { get; set; }
public String Column_Description { get; set; }
public Boolean IsAnswer { get; set; }
}
public class MatrixRows
{
public int Row_Id { get; set; }
public String Row_Number { get; set; }
public String Row_Description { get; set; }
}
当我填满我的模型并尝试使用DisplayTemplate显示MatrixRows时,它不会显示任何内容。我的DisplayTemplate的名称是MatrixRows.cshtml,它放在Views / Shared / DisplayTemplates
中这是我用来显示MatrixRow的代码:
@model TestInheritance.Models.ShowMatrixQuestionViewModel
@using (Html.BeginForm())
{
<div id="edit">
<h2>Columns</h2>
@foreach (var column in Model.columns)
{
<p>@column.GetType().Name</p>
Html.RenderPartial("EditMatrixColumn", column);
}
<h2>Rows</h2>
@foreach (var rows in Model.rows)
{
<p>@rows.GetType()</p>
Html.DisplayFor(x => rows);
//Html.RenderPartial("EditMatrixRow", rows);
}
</div>
<input type="submit" value="Finished" />
}
当我使用RenderPartial时它工作正常......我做错了什么?
DisplayTemplate的代码:
@model TestInheritance.Models.MatrixRows
@using TestInheritance.Helpers
<div class="editrow">
@using (Html.BeginCollectionItem("rows"))
{
<span>
Nummer:
@Html.EditorFor(cn => Model.Row_Number)
</span>
<br />
<span>
Beskrivelse:
@Html.EditorFor(bs => Model.Row_Description)
</span>
}
</div>
答案 0 :(得分:5)
Html.DisplayFor
返回HTML
您没有对该HTML做任何事情。
您可能希望通过编写@Html.DisplayFor(...)
答案 1 :(得分:2)
而不是写循环:
@foreach (var rows in Model.rows)
{
<p>@rows.GetType()</p>
Html.DisplayFor(x => rows);
}
简单地:
@Html.DisplayFor(x => x.rows)
然后在相应的显示模板~/Views/Shared/DisplayTemplates/MatrixRows.cshtml
内,它将自动为集合的每个元素呈现:
@using TestInheritance.Helpers
@model TestInheritance.Models.MatrixRows
<p>@GetType()</p>
<div class="editrow">
@using (Html.BeginCollectionItem("rows"))
{
<span>
Nummer:
@Html.EditorFor(x => x.Row_Number)
</span>
<br />
<span>
Beskrivelse:
@Html.EditorFor(x => x.Row_Description)
</span>
}
</div>
这就是说,您的显示模板看起来更像是一个编辑器模板,因为它包含输入字段。所以:
@Html.EditorFor(x => x.rows)
然后使用~/Views/Shared/EditorTemplates/MatrixRows.cshtml