部分视图在ajax更新后不会呈现
本文关键字:更新 视图 ajax | 更新日期: 2023-09-27 18:11:40
asp MVC 3.
在初始渲染,一切都是伟大的。我输入一个我知道存在的搜索字符串并单击搜索按钮,部分视图(_searchresult)消失。我在开发人员工具的网络选项卡中进行了测试,我看到ajax按照预期返回结果。因此ajax调用得到了正确的结果,但没有呈现。我去了
localhost/home/_searchresult
但是它只显示[]。
视图:
@model Tracker.Models.PaginatedList<TrespassTracker.Models.PersonViewModel>
<div id="search-index">
<div class="editor-field">
<label>First Name:</label>
@Html.TextBox("FirstName")
<label style = "margin-left: 15px;">Last Name:</label>
@Html.TextBox("LastName", "", new { style = "margin-right: 15px;" })
</div>
<div id="search-controls-index">
<input type="button" id="searchbtn" class="skbutton" value="Search" />
<input type="button" id="addPersonbtn" class="skbutton" value="Add New Person" onclick="location.href='@Url.Action("AddPerson", "Person")'"/>
</div>
</div>
<div id="result-list-index">
@Html.Partial("_SearchResult", Model)
</div>
<div id="paging-controls">
<div id="paging-controls-left">
@{ if(Model.HasPreviousPage)
{
@Html.ActionLink("<< Previous", "Index", new { page = (Model.PageIndex - 1) });
}
if (Model.HasNextPage)
{
@Html.ActionLink("Next >>", "Index", new { page = (Model.PageIndex + 1) });
}
}
</div>
<div id="paging-controls-right">
@{ int PageNumber = @Model.PageIndex + 1; }
Page: @PageNumber of @Model.TotalPages
</div>
</div>
</div>
jquery: $(document).ready(function(){
$("#searchbtn").on('click', function () {
var fsname = $("#FirstName").val();
var ltname = $("#LastName").val();
$.ajax({
type: 'GET',
url: "Home/_SearchResult",
data: { fname: fsname, lname: ltname },
success: function (data) {
$("#result-list-index").html(data);
},
error: function () {
$("#result-list-index").html("An error occurred while trying to retrieve your data.");
}
});
});
});
控制器:
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);
}
========== 编辑 ===========
我认为_searchresult方法是错误的,所以我把它改成了PartialResult方法:
public PartialViewResult _SearchResult(string fname, string lname)
{
var peopleList = repo.GetSearchResult(fname, lname);
//return Json(peopleList, JsonRequestBehavior.AllowGet);
return PartialView("_SearchResult");
}
现在部分在索引页上呈现,但它返回由于内部错误500而导致的失败通知。我对它进行了跟踪,并在部分视图中的模型上发现了一个null错误。这是偏导数。指示的错误位置在foreach,对象未设置为实例…我认为这意味着它返回一个null模型。
@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>
你不是返回偏视图,你是返回数据。要返回部分视图,您必须执行以下操作(注意,我们没有返回JSON):
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);
}