2

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

控制器:

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));
    }
}

部分视图:

@(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 的响应:

{"Data":[],"Total":0,"AggregateResults":null,"Errors":null}

(哪个是对的)

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

这个问题并不完全是一个骗局。以前曾问过非常相似的问题,但没有一个将附加值传递给控制器​​的读取函数(已正确填充),据我所知,我已经实现了这些问题和剑道网站没有成功。

这必须是基本的东西。有任何想法吗?

谢谢!

4

3 回答 3

0

尝试[AcceptVerbs(HttpVerbs.Post)]从 ActionMethod 中删除属性

于 2014-01-30T20:55:33.067 回答
0

您可以将 JSON 数据从 Kendo 网格发送到 mvc 控制器您需要按照以下步骤操作

  1. Stringfy Json数据并通过ajax调用发送数据
  2. 在控制器中接收数据作为列表

     var serviceUrl = '/Enrollment/EnrollCourse';
                //Pass the items from the grid to the controll. Enroll a user for a course
                 $.ajax({
                    type: "POST",
                    url: serviceUrl,
                    data: JSON.stringify($("#grid").data("kendoGrid").dataItems()),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                 }).done(function (data, textStatus, request) {
                     alert('success.');
                    $("#grid").data('kendoGrid').dataSource.data([]);
    
                 }).fail(function (jqXHR, textStatus, errorThrown) {
                  alert('Error.');
                });
    

    [HttpPost] public ActionResult EnrollCourse(IEnumerable 注册) { return Json(JsonRequestBehavior.AllowGet); }

        Note: Do not try to corrupt JSON data as mentioned by Kendo UI admin
        data: JSON.stringify({ Technicians:("#Technicians").data("kendoMultiSelect").dataItems() })
    
于 2015-08-15T22:06:16.510 回答
0

尝试在控制器中添加“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);
                }
            }
于 2016-09-01T07:06:32.473 回答