所以我对控制器的 Json 调用有问题。我收到一个错误“parseerror - Unexpected token <”
在我看来,我的代码如下:
$("select[name='SelectedProjectStatus']").change(function () {
var DashboardModel = {
SelectedProjectStatus: $("select[name='SelectedProjectStatus']").val(),
Page: 1
};
$.ajax({
url: '@Url.Action("Dashboard", "Dashboard")',
type: 'POST',
data: JSON.stringify(DashboardModel),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
$('#DashboardResult').replaceWith(data);
},
error: function (request, status, err) {
alert(status);
alert(err);
}
});
});
在我的控制器中:
[HttpPost]
public ActionResult Dashboard(DashboardModel dashboard)
{
var MyProjects = EFProject.Project.Where(x => x.UserID == 1);
if (dashboard.SelectedProjectStatus != 0)
MyProjects = MyProjects
.Where(x => x.Status == dashboard.SelectedProjectStatus)
.OrderByDescending(p => p.AuditingCD)
.Skip((dashboard.Page - 1) * PageSize)
.Take(PageSize);
else
MyProjects = MyProjects
.OrderByDescending(p => p.AuditingCD)
.Skip((dashboard.Page - 1) * PageSize)
.Take(PageSize);
DashboardModel model = new DashboardModel
{
Projects = MyProjects,
PagingInfo = new PagingInfo
{
CurrentPage = dashboard.Page,
ItemsPerPage = PageSize,
TotalItems = EFProject.Project.Where(x => x.UserID == 1).Count()
}
};
if (Request.IsAjaxRequest())
{
return PartialView("DashboardResult", model);
}
else
{
return View(model);
}
}
我试图渲染的部分视图只有以下内容:
@using BidThatProject.Web.Models.NonReusableModels.ProjectManagment
@model DashboardModel
<div id="DashboardResult">
</div>
并且父视图显然具有具有相同 id 的 div。它至少应该通过在我身上显示一个空白空间来工作,但它似乎在将数据解析回视图时遇到了问题。可能是它没有将其作为 application/json 类型读取?我真的不知道。