C# 部分视图在ajax更新后不会渲染
asp mvc 3 在初始渲染时,一切都很好。我输入一个我知道存在的搜索字符串,然后单击搜索按钮,部分视图(_searchresult)将消失。我在开发工具的网络选项卡中进行了测试,我看到ajax按预期返回结果。因此,ajax调用会得到正确的结果,但不会渲染。我去了C# 部分视图在ajax更新后不会渲染,c#,asp.net-mvc,jquery,razor,C#,Asp.net Mvc,Jquery,Razor,asp mvc 3 在初始渲染时,一切都很好。我输入一个我知道存在的搜索字符串,然后单击搜索按钮,部分视图(_searchresult)将消失。我在开发工具的网络选项卡中进行了测试,我看到ajax按预期返回结果。因此,ajax调用会得到正确的结果,但不会渲染。我去了 localhost/home/_searchresult 但它所显示的只是[] 视图: 控制器: public ActionResult Index(int? page) { const int
localhost/home/_searchresult
但它所显示的只是[]
视图:
控制器:
public ActionResult Index(int? page)
{
const int PAGESIZE = 10;
var peopleList = repo.GetPeople();
var pagedPeopleList = new PaginatedList<PersonViewModel>(peopleList, page ?? 0, PAGESIZE);
return View(pagedPeopleList);
}
public JsonResult _SearchResult(string fname, string lname)
{
var peopleList = repo.GetSearchResult(fname, lname);
return Json(peopleList, JsonRequestBehavior.AllowGet);
}
现在,部分在索引页上呈现,但由于内部错误500,它返回失败通知。我找到了它,在部分视图中发现模型上有一个空错误。这是部分原因。指示的错误位置位于foreach,对象未设置为实例。。。我相信这意味着它将返回一个空模型
@model Tracker.Models.PaginatedList<TrespassTracker.Models.PersonViewModel>
<table class="data-table">
<tr>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Gender
</th>
<th>
DOB
</th>
<th>
School
</th>
<th>
IsStudent
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.DOB)
</td>
<td>
@Html.DisplayFor(modelItem => item.School)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsStudent)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
@model Tracker.Models.PaginatedList
名字
姓氏
性别
出生日期
学校
学生
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.FirstName)
@DisplayFor(modelItem=>item.LastName)
@DisplayFor(modeleItem=>item.Gender)
@DisplayFor(modeleItem=>item.DOB)
@DisplayFor(modeleItem=>item.School)
@DisplayFor(modelItem=>item.IsStudent)
@ActionLink(“编辑”,“编辑”,新的{/*id=item.PrimaryKey*/})|
@ActionLink(“详细信息”,“详细信息”,新的{/*id=item.PrimaryKey*/})
}
您不是返回局部视图,而是返回数据。要返回部分视图,您必须执行以下操作(注意,我们没有返回JSON):
我很困惑。您是否正在调用返回JSON的
\u SearchResult
控制器操作,并希望它使用该操作作为模型重新呈现局部视图?@Jacob,他正试图以这种方式调用局部视图。当他把它称为上面的一部分时,它起了作用。Tehou似乎认为返回json的action方法也将作为其partial的子方法,但这不是真的。@Tehou,在您最近的尝试中,您需要将模型传递到对PartialView
的调用中,类似于@MattyMo的代码。很酷,谢谢,我打败了她。问题确实出在模型上。我已经手动编码了分页,并且正在使用一个自定义类,我必须将其传递给部分视图并返回一个新的分页列表视图。谢谢你们让我走上正轨。
public ActionResult Index(int? page)
{
const int PAGESIZE = 10;
var peopleList = repo.GetPeople();
var pagedPeopleList = new PaginatedList<PersonViewModel>(peopleList, page ?? 0, PAGESIZE);
return View(pagedPeopleList);
}
public JsonResult _SearchResult(string fname, string lname)
{
var peopleList = repo.GetSearchResult(fname, lname);
return Json(peopleList, JsonRequestBehavior.AllowGet);
}
public PartialViewResult _SearchResult(string fname, string lname)
{
var peopleList = repo.GetSearchResult(fname, lname);
//return Json(peopleList, JsonRequestBehavior.AllowGet);
return PartialView("_SearchResult");
}
@model Tracker.Models.PaginatedList<TrespassTracker.Models.PersonViewModel>
<table class="data-table">
<tr>
<th>
FirstName
</th>
<th>
LastName
</th>
<th>
Gender
</th>
<th>
DOB
</th>
<th>
School
</th>
<th>
IsStudent
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.DOB)
</td>
<td>
@Html.DisplayFor(modelItem => item.School)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsStudent)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
public ActionResult _SearchResult(string fname, string lname)
{
var peopleList = repo.GetSearchResult(fname, lname);
//Is peopleList the right model type? If not, create your model here
return View(peopleList);
}