简体   繁体   中英

Model Binding fails with Kendo Grid

I can't use edit and create command of a Kendo Grid, because binding always fails and controller always receives a null object, even though DataSourceRequest is fine.

I used a @html.EditorForModel() and it worked fine and controller received the data. So MVC Model binding and my class aren't the problem.

Also, using F12, I can see the data being posted and it's fine too.

Any Idea about possible problem and debugging? There is an issue with all of my Kendo Grids, sometimes they post a model twice and I receive double Create on the server, which one of them always fails because of repetitive Keys. Or sometimes create will be triggered by edit command too.

I have no idea where I'm going wrong, in most scenarios the user will choose the primary key as well, so my returned model doesn't need an update.

With this particular class, server always receives two create and none of them binds correctly. The first create, posts all of the fields, and the second posts all of the navigation properties' fields too!

This is the action method:

[HttpPost]
public ActionResult Create([DataSourceRequest] DataSourceRequest request, Stock stock){
        if (stock != null && ModelState.IsValid)
        {
            repo.Insert(stock);
            return Json(new[] { stock }.ToDataSourceResult(request, ModelState));
        }
        else
            return Json(null);
}

And the Grid:

@(Html.Kendo().Grid<Stock>()
    .Name("stock")
        .Columns(columns =>
        {
            columns.ForeignKey(x => x.CGoodCode, (IEnumerable)ViewData["Goods"], "Id", "Text");
            ///
            columns.Command(command => { command.Edit(); command.Destroy(); });
        })
        .ToolBar(toolbar => {
            toolbar.Create();
            toolbar.Excel();
            toolbar.Pdf();
        })
        .Editable(edit => edit.Mode(GridEditMode.PopUp))
        .Sortable()
        .Pageable(p => p.PageSizes(true))
        .DataSource(ds => 
            ds.Ajax()
            .Model(model => {
                model.Id(x => x.CGoodCode);
                model.Id(x => x.SWhCode);
                model.Id(x => x.LiIdPeriod);
                model.Field(x => x.CGoodCode).DefaultValue(ViewData["GoodsDefault"]);
                ////

            })
            .Read("Read", "Stock")
            .Update("Edit", "Stock")
            .Create("Create", "Stock")
            .Destroy("Delete", "Stock")
        )
)

UPDATE

It seems Kendo Grid doesn't support composite primary keys yet!

This question helped me out! I just need to rename the parameter like:

public ActionResult Create([DataSourceRequest] DataSourceRequest request, Stock somethingElse){

Because my class contains some fields with that name, Stock!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM