我有JsonResult的问题,因为我是Json和MVC的新手,因此,我将JsonResult从Controller传递给View:
控制器:
public ActionResult Index()
{
return View(Json(_studentService.GetAllStudents(), JsonRequestBehavior.AllowGet));
}
查看:
@model IEnumerable<StudentApp.Models.Student>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<fieldset>
<legend>Student</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.name)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.name)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.id }) |
@Html.ActionLink("Back to List", "Index")
</p>
</body>
</html>
但是,在View中我得到了例外。
答案 0 :(得分:1)
仅通过
public ActionResult Index()
{
return View(_studentService.GetAllStudents());
}
在视图中使用
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayNameFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}