C# 我应该如何将JSONResult类型强制转换为IEnumerable
我对JsonResult有一个问题,因为我是Json和MVC新手,所以我将JsonResult从控制器传递到视图: 控制器:C# 我应该如何将JSONResult类型强制转换为IEnumerable,c#,asp.net-mvc,json,C#,Asp.net Mvc,Json,我对JsonResult有一个问题,因为我是Json和MVC新手,所以我将JsonResult从控制器传递到视图: 控制器: public ActionResult Index() { return View(Json(_studentService.GetAllStudents(), JsonRequestBehavior.AllowGet)); } @model IEnumerable<StudentApp.Models.Student> @{ Layout =
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>
查看:
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>
@model IEnumerable
@{
布局=空;
}
指数
学生
@DisplayNameFor(model=>model.name)
@DisplayFor(model=>model.name)
@ActionLink(“编辑”,“编辑”,新的{id=Model.id})|
@ActionLink(“返回列表”、“索引”)
但是,鉴于此,我得到了一个例外。仅通过
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>
}
@foreach(模型中的变量项)
{
@DisplayNameFor(modelItem=>item.Name)
@DisplayFor(modelItem=>item.Name)
}
为什么不返回视图(\u studentService.GetAllStudents());我已成功删除异常,但现在我得到的“System.Collections.Generic.IEnumerable”不包含“name”的定义,并且找不到接受“System.Collections.Generic.IEnumerable”类型的第一个参数的扩展方法“name”(是否缺少using指令或程序集引用?)如果您要传递Ienumerable,这意味着要使用@Html.DisplayNameFor(model=>model[0].name)等等on@NitinVarpe我发现无法将[]索引应用于类型为“System.Collections.Generic.IEnumerableplease check updated answerYeah,它起作用了:),但我只是想知道是否必须使用前面的场景,如果我想用JSON函数返回结果,那么我必须做什么,你能告诉我..JSON数据返回到ajax成功调用。这是什么意思?请详细说明我是JSONI的新手在今天的web世界中,你必须了解jquery ajax以及它在mvc3中的使用。检查这个小示例,它在哪里使用感谢Nitin,我是SQL开发人员,但突然我不得不在前端工作,所以现在我在前端工作时不得不面对很多麻烦。