模型绑定失败,剑道网格 [英] Model Binding fails with Kendo Grid

查看:146
本文介绍了模型绑定失败,剑道网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能使用编辑和创建剑道电网命令,因为总是绑定失败和控制器总是收到一个对象,即使 DataSourceRequest 的罚款。

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.

我用了一个 @ html.EditorForModel(),它能正常工作和控制器接收到的数据。因此MVC模型绑定和我的课都没有问题。

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.

此外,使用F12,我可以看到数据被发布,这也没关系。

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!

这是操作方法:

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

和网格:

@(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")
        )
)

更新

看来剑道电网不支持复合主键呢!

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

推荐答案

<一个href=\"http://stackoverflow.com/questions/15549495/model-binding-to-action-is-always-null-if-the-parameter-is-named-model-for-ken\">This问题帮我出去!我只需要像重命名参数:

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!

这篇关于模型绑定失败,剑道网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆