Json请求parseerror - 意外令牌< MVC3

时间:2012-04-17 20:54:23

标签: jquery ajax json asp.net-mvc-3 partial

所以我的Json调用控制器出了问题。我得到一个错误 “parseerror - 意外的标记<”

我的代码如下所示:

$("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类型读取?我真的不知道。

1 个答案:

答案 0 :(得分: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);
    }
});