[英]Partial view wont render after ajax update
asp mvc 3. ASP MVC 3。
On initial render, all is great. 在初始渲染时,一切都很好。 I enter a search string that i know exists and click the search button, the partial view (_searchresult) disappears.
我输入一个我知道存在的搜索字符串,然后单击搜索按钮,部分视图(_searchresult)消失了。 I tested in the network tab of the developer tool and I see ajax returns the results as expected.
我在开发人员工具的“网络”标签中进行了测试,然后看到ajax返回预期的结果。 So the ajax call gets the correct results but does not render.
因此,ajax调用会获得正确的结果,但不会呈现。 I went to
我去了
localhost/home/_searchresult
but all it displays is []. 但显示的只是[]。
View: 视图:
@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: 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.");
}
});
});
});
Controller: 控制器:
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);
}
==========EDIT=========== ==========编辑===========
I assume by the comments the _searchresult method is wrong, so I changed it to a PartialResult one: 我认为注释中_searchresult方法是错误的,因此我将其更改为PartialResult之一:
public PartialViewResult _SearchResult(string fname, string lname)
{
var peopleList = repo.GetSearchResult(fname, lname);
//return Json(peopleList, JsonRequestBehavior.AllowGet);
return PartialView("_SearchResult");
}
Now the partial renders on the index page, but it returns the failure notice due to an internal error 500. I tracked it down and found a null error on the Model in the partial view. 现在,部分渲染在索引页面上,但是由于内部错误500,它返回了失败通知。我对其进行了跟踪,并在部分视图中的Model上找到了空错误。 Here is the partial.
这是局部的。 The indicated error location is at the foreach, object not set to an instance... which I believe means it is returning a null Model.
指示的错误位置在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>
You're not returning the Partial View, you're returning the data. 您没有返回Partial View,而是返回了数据。 To return the Partial View, you'd have to do something like (note that we're not sending back JSON):
要返回部分视图,您必须执行类似的操作(请注意,我们没有发送回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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.