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,它返回了失败通知。我跟踪它,在部分视图中发现模型上有一个空错误。这里是部分。指示的错误位置位于 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>