ViewData.TemplateInfo.TemplateDepth > 1
而不是打电话
@Html.DisplayForModel("ApplicationOutcome")
在您的视图中调用另一个模板。放
foreach(var item in Model)
{
@Html.DisplayFor(m=>item)
}
在你的视图中直接。这应该可以得到预期的结果。
下面是我的 MVC Controller/Model/View 文件。你能试试这些吗?我没有任何特定于模型的模板文件,这是关键。会发生什么是 mvc 将使用默认模板来渲染该对象。
DefaultController.cs(控制器)
public class DefaultController : Controller
{
//
// GET: /Default/
public ActionResult Index()
{
var model = new List();
model.Add(new Applicant{FirstName = "foo",LastName = "bar", Address = "Foo Bar"});
model.Add(new Applicant { FirstName = "foo1", LastName = "bar1", Address = "Foo1 Bar1" });
model.Add(new Applicant { FirstName = "foo2", LastName = "bar2", Address = "Foo2 Bar2" });
return View(model);
}
}
Application.cs(型号)
public class Applicant
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
}
Index.cshtml(查看)
@model IEnumerable
@{
Layout = null;
}
this is a testThis is a test
@if (Model != null)
{
foreach (var item in Model)
{
@Html.DisplayFor(m => item)
}
}
结果输出