我想在我的視圖中顯示包含多個字段的文章,但它不起作用:字段不顯示(只有標題(字段的名稱))。無法顯示嵌套IEnumerable的編輯器模板
Article類:
public class Article : IComparable
{
public IEnumerable<ArticleField> Fields
{
get;
set;
}
[More properties...]
}
ArticleField類:
public class ArticleField
{
// !! I want this display in a table Header !!
[Display(Name = "Name", ResourceType = typeof(Resources.Classes.ArticleField))]
[Required(ErrorMessageResourceName="required", ErrorMessageResourceType=typeof(Resources.Common.Validation))]
public string Name
{
get;
set;
}
[More properties...]
}
Index.cshtml(似乎罰款渲染HTML)
@model MygLogWeb.Classes.Article.Article
@using (Html.BeginForm("Index", "Article", FormMethod.Post))
{
@Html.AntiForgeryToken()
<table class="table">
<tbody>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<td>
@if (Model.IsSystem)
{
@Html.HiddenFor(model => model.Name)
@Model.Name
}
else
{
@Html.EditorFor(model => model.Name)
}
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.IsSystem)
</th>
<td>
@Html.HiddenFor(model => model.IsSystem)
@Model.IsSystem
</td>
</tr>
</tbody>
</table>
<br style="margin: 0.5em 0em;"/>
@* POINT OF INTEREST *@
@Html.EditorFor(model => model.Fields, "ArticleFields")
<button type="submit" name="button" value="save">@Resources.Common.Buttons.save</button>
}
EditorTemplates/ArticleFields.cshtml(出現罰款呈現html)
@model IEnumerable<MygLogWeb.Classes.Article.ArticleField>
@using MygLogWeb.Classes.Article
@{
var uid = Guid.NewGuid().ToString();
var GlobalViewBag = ViewContext.Controller.ViewBag;
GlobalViewBag.ItemCount = Model.Count();
}
<table class="table none" id="@uid">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.IsSystem)
</th>
<th>
@Html.DisplayNameFor(model => model.IsVirtual)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Formula)
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
@* POINT OF INTEREST *@
@Html.EditorFor(model => model)
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5">
<input type="button" value="@Resources.Common.Buttons.add" data-row-add="ArticleField" data-row-addto="#@uid > TBODY" />
</td>
</tr>
</tfoot>
</table>
<script type="text/javascript">
new TableEdit("@uid");
</script>
EditorTemplates/ArticleFields.cshtml(根本不會出現渲染HTML)
@model MygLogWeb.Classes.Article.ArticleField
xxx
不過,我覺得我可以做一個public IEnumerable<ArticleField> Fields
但List<T>
然後我會得到一個問題與頭。諸如@Html.DisplayNameFor(model => model.Formula)
之類的代碼不起作用。
東西看起來不對我。在你的'EditorTemplates/ArticleFields.cshtml'模板中,你將你的模型聲明爲一個'IEnumerable',但是你永遠不會迭代低谷。爲什麼這樣? –
據我所知,我無法迭代。 []運算符在IEnumerable上不存在,並且對於正確命名的字段是必需的。所以我必須讓MVC處理IEnumerable,就像它通常在您的main(Index.cshtml)模型是IEnumerable時所做的那樣。 – Serge
然後使用'ToList()'。 –