如何将视图返回给AJAX调用?现在我用我的控制器方法用AJAX jQuery做一个get请求,但它命中了error
处理程序。
我在视图中有这个下拉列表:
<select id="linkDocumentDropDown">
<option value="ten">10</option>
<option value="twentyfive">25</option>
<option value="all">all result</option>
</select>
这个AJAX调用我的控制器返回视图:
$.ajax({
url: "/Home/SearchResult",
type: "GET",
data: {
searchString: $("#SearchString").val(),
pageSize: 25
},
complete: function (xData, status) {
$("#test").html(xData);
},
error: function (status) {
alert(status.message);
}
})
我的控制员:
public async Task<ActionResult> SearchResult(string searchString, int pageSize)
{
QuestionAnswerService service = new QuestionAnswerService();
SearchedQuestionsResponse searchResults = new SearchedQuestionsResponse();
bool isSufficientLength = false;
string[] searchTerms = searchString.Split(' ');
foreach (string term in searchTerms)
{
if (term.Trim().Length > 2)
{
isSufficientLength = true;
break;
}
}
if (!string.IsNullOrEmpty(searchString) && isSufficientLength)
{
searchResults = await service.GetSearchQuestions(HttpContext.User.Identity.Name, searchString);
Session["SearchResults"] = searchResults;
if (searchResults.searchedquestions == null || searchResults.searchedquestions.Count == 0)
{
ViewBag.message = Messages.NoResults;
}
else
{
ViewBag.searchtext = Messages.SearchResults + " " + searchString;
Session["SearchText"] = Messages.SearchResults + " " + searchString;
}
}
else
{
ViewBag.message = Messages.SearchStringInvalid;
}
// int pageSize = PAGE_SIZE;
int pageNumber = PAGE_INDEX;
return View(searchResults.searchedquestions.ToPagedList(pageNumber, pageSize));
}