-1

所以我对控制器的 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 类型读取?我真的不知道。

4

1 回答 1

1

使用 Json Result 并返回 Json(model);

public JsonResults MyTest(){
    var model = FROM b in Table WHERE b.id == 1 select b;
    return Json(model);
}

$.ajax({
        url: '@Url.Action("Dashboard", "Dashboard")',
        type: 'POST',
        data: {page: 1, selected: $("select[name='SelectedProjectStatus']").val() },
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (data) { // 
            // process the json object into html page
        },
        error: function (request, status, err) {
            alert(status);
            alert(err);
        }
    });

这是一个编辑,因为博客文章是通过 ajax 获取部分视图,而不是通过 ajax 获取 json 数据:

public ActionResults MyTest(SearchModel  results){
    var model = FROM b in Table WHERE b.id == 1 select b;
    return PartialView("ViewName",model);
}
// you create a partial view that takes in your model
$.ajax({
    url: '/ajax/MySearch',
    type: 'POST',
    data: $("form").serialize(),        
    success: function (data) { // 
        $("#searchresults").html(data);
    },
    error: function (request, status, err) {
        alert(status);
        alert(err);
    }
});
于 2012-04-17T21:21:06.690 回答