Reputation: 3397
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. I tested in the network tab of the developer tool and I see ajax returns the results as expected. So the ajax call gets the correct results but does not render. 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:
$(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:
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. 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.
@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>
Upvotes: 1
Views: 4462
Reputation: 56429
You're not returning the Partial View, you're returning the data. To return the Partial View, you'd have to do something like (note that we're not sending back 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);
}
Upvotes: 2