MVC控制器传递JSON数据,但Kendo Grid没有显示它

时间:2013-08-29 16:02:09

标签: ajax json kendo-ui kendo-grid kendo-asp.net-mvc

我在Razor PartialView中运行了一些(动态分配的)Kendo网格。网格旨在通过MVC控制器的AJAX请求其数据。控制器接收读取请求,请求良好,控制器通过线路将数据传回(通过Fiddler验证)。    不幸的是,网格从不显示数据。我是新手,不知道从哪里开始。据我所知,代码与此场景的Kendo示例相匹配。这不是数据或模型问题,因为两者都在应用程序的其他部分中使用而没有问题。

控制器:

public class MemberController : Controller
{
    private MemberContext db = new MemberContext();
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Grid_Member_Read([DataSourceRequest] DataSourceRequest request,
        int club_id)
    {
        var results = from r in db.Members where r.MemberClub.Id == club_id select r;
        return Json(results.ToDataSourceResult(request));
    }
}

PartialView:

@(Html.Kendo().Grid<RegistrationManagement.Models.Member>()
.Name("MembersGrid")
.Columns(columns =>
{
    columns.Bound(p => p.LastName);
    columns.Bound(p => p.FirstName);
    columns.ForeignKey(p => p.MemberStatusID, (System.Collections.IEnumerable)ViewData["status"], "Value", "Text");
    columns.Bound(p => p.RegistrationYear);
    columns.Bound(p => p.StreetAddress1);
    columns.Bound(p => p.StreetAddress2);
    columns.Bound(p => p.State);
    columns.Bound(p => p.ZipCode);
    columns.Bound(p => p.Email);
    columns.Bound(p => p.PhoneNumber);
    columns.Command(command => { command.Custom("Edit2").Click("CustomEdit"); command.Edit(); command.Destroy(); }).Width(160);

})
        .ToolBar(toolbar => toolbar.Create())
.Editable()
.Groupable()
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .Batch(true)
    .ServerOperation(false)
    .Events(events => events.Error("error_handler"))
    .Model(model =>
        {
            model.Id(p => p.Id);
            model.Field(p => p.MemberStatus).Editable(false);
        })
    .Read(read => read
        .Action("Grid_Member_Read", "Member", new { club_id = @ViewBag.ClubID })

    )
    .Create(create => create.Action("Grid_Member_Create", "Member", new { ClubID = @ViewBag.ClubID }))
    .Update(update => update.Action("Grid_Member_Update", "Member"))
    .Destroy(destroy => destroy.Action("Grid_Member_Destroy", "Member"))
 )
)

function error_handler(e) {
    if (e.errors) {
        var message = "Errors:\n";
        $.each(e.errors, function (key, value) {
            if ('errors' in value) {
                $.each(value.errors, function () {
                    message += this + "\n";
                });
            }
        });
        alert(message);
    }
}
function CustomEdit(e) {
    e.preventDefault();

    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));

}

小提琴手数据:

控制器对Grid 1的读取请求的响应:

{"Data":[{"MemberClub":{"ClubRegion":{"Id":0,"Code":"TE","Name":"Test"},"ClubStatus":       {"Id":0,"Name":"Unknown"},"RelationshipManager":{},"Id":1,"ClubName":"B     Club","RegistrationYear":2000,"StreetAddress1":"street","StreetAddress2":"a","City":"city","State":"state","ZipCode":11111,"PhoneNumber":"555-1212","Email":"a@a.com","URL":"www.w.w."},"MemberStatus":{"Id":2,"Name":"Active"},"Id":1,"MemberClubID":1,"MemberStatusID":2,"MembershipNumberRoot":1,"LastName":"last","FirstName":"Edited","StreetAddress1":"street","StreetAddress2":"a","City":"city","State":"state","ZipCode":"11111","PhoneNumber":"555-1212","Email":"a@a.com","RegistrationYear":2000},{"MemberClub":{"ClubRegion":{"Id":0,"Code":"TE","Name":"Test"},"ClubStatus":{"Id":0,"Name":"Unknown"},"RelationshipManager":{},"Id":1,"ClubName":"B Club","RegistrationYear":2000,"StreetAddress1":"street","StreetAddress2":"a","City":"city","State":"state","ZipCode":11111,"PhoneNumber":"555-1212","Email":"a@a.com","URL":"www.w.w."},"MemberStatus":{"Id":10,"Name":"New"},"Id":10,"MemberClubID":1,"MemberStatusID":10,"MembershipNumberRoot":null,"LastName":"1","FirstName":"1","StreetAddress1":"1","StreetAddress2":"1","City":null,"State":"1","ZipCode":"1","PhoneNumber":"1","Email":"1","RegistrationYear":1},{"MemberClub":{"ClubRegion":{"Id":0,"Code":"TE","Name":"Test"},"ClubStatus":{"Id":0,"Name":"Unknown"},"RelationshipManager":{},"Id":1,"ClubName":"B Club","RegistrationYear":2000,"StreetAddress1":"street","StreetAddress2":"a","City":"city","State":"state","ZipCode":11111,"PhoneNumber":"555-1212","Email":"a@a.com","URL":"www.w.w."},"MemberStatus":{"Id":10,"Name":"New"},"Id":11,"MemberClubID":1,"MemberStatusID":10,"MembershipNumberRoot":null,"LastName":"2","FirstName":"2","StreetAddress1":"2","StreetAddress2":"2","City":null,"State":"2","ZipCode":"2","PhoneNumber":"2","Email":"2","RegistrationYear":2}],"Total":3,"AggregateResults":null,"Errors":null}

控制器对Grid 2读取请求的响应:

{"Data":[{"MemberClub":{"RelationshipManager":{},"Id":2,"ClubName":"Test Club","RegistrationYear":2000,"StreetAddress1":"street","StreetAddress2":"a","City":"city","State":"state","ZipCode":11111,"PhoneNumber":"555-1212","Email":"a@a.com","URL":"www.w.w.","ClubRegion":{"Id":0,"Code":"TE","Name":"Test"},"ClubStatus":{"Id":0,"Name":"Unknown"}},"MemberStatus":{"Id":10,"Name":"New"},"Id":13,"MemberClubID":2,"MemberStatusID":10,"MembershipNumberRoot":2,"LastName":"Dude","FirstName":"Random","StreetAddress1":"street","StreetAddress2":"a","City":"City","State":"State","ZipCode":"11111","PhoneNumber":"555-1212","Email":"b@b.com","RegistrationYear":2012}],"Total":1,"AggregateResults":null,"Errors":null}

对网格3的响应:

{ “数据”:[], “总计”:0, “AggregateResults”:NULL, “错误”:空}

(这是正确的)

在我开始显示多个网格之前,我有一个网格,它使用从控制器传递的模型来包含数据。这非常有效。然后我添加了动态网格创建(三个网格都使用了传递的模型)并且它们都显示了所请求的最后一个模型数据,所以我开始使用AJAX方法。

这个问题不是一个愚蠢的问题;之前已经提出了非常类似的问题,但是这些问题都没有传递给控制器​​的读取功能(正确填充),并且据我所知,我已经实现了这些问题和剑道网站没有成功。

这必须是基本的基础。有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

尝试从ActionMethod

中删除[AcceptVerbs(HttpVerbs.Post)]属性

答案 1 :(得分:0)

尝试在控制器reas操作方法中添加“JsonRequestBehavior.AllowGet”。而且无需添加“[AcceptVerbs(HttpVerbs.Post)]”。

            public class MemberController : Controller
            {
                private MemberContext db = new MemberContext();                    
                public ActionResult Grid_Member_Read([DataSourceRequest] DataSourceRequest request,
                    int club_id)
                {
                    var results = from r in db.Members where r.MemberClub.Id == club_id select r;
                    return Json(results.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
                }
            }