Javascript Kendo ui aspnet mvc网格绑定json对象从操作返回到新行

Javascript Kendo ui aspnet mvc网格绑定json对象从操作返回到新行,javascript,c#,json,asp.net-mvc,kendo-ui,Javascript,C#,Json,Asp.net Mvc,Kendo Ui,我的网格上有自动完成控制。从autocomplete中选择一个元素后,我调用一个事件选择“onSelectArticle”,使用LineBonLivraison\u Add操作导入该对象,并希望将其绑定为json对象,而不仅仅是将值设置为列 问题只发生在新添加的行上。例如,当我编辑现有行时,我可以获取所选对象的属性并为其设置值,如(var item=grid.dataItem(select);item.set(“Document”,data.Document);),但对于新行,“item”为空

我的网格上有自动完成控制。从autocomplete中选择一个元素后,我调用一个事件选择“onSelectArticle”,使用LineBonLivraison\u Add操作导入该对象,并希望将其绑定为json对象,而不仅仅是将值设置为列

问题只发生在新添加的行上。例如,当我编辑现有行时,我可以获取所选对象的属性并为其设置值,如(var item=grid.dataItem(select);item.set(“Document”,data.Document);),但对于新行,“item”为空

@section LinesTab {
<style>
    .k-widget .templateCell
    {
        overflow: visible;
    }
</style>
<script>
    function initMenus(e) {
        $(".templateCell").each(function () {
            eval($(this).children("script").last().html());
        });
    }

    function onEditGrid(editEvent) {
        // Ignore edits of existing rows.
        if (!editEvent.model.isNew() && !editEvent.model.dirty) {
            //alert("not new dirty")
            return;
        }

        editEvent.container
            .find("input[name=Document]") // get the input element for the field
            .val("100") // set the value
            .change(); // trigger change in order to notify the model binding
    }
</script>

<div class="lines-tab-doc">
    @(Html.Kendo().Grid<LineBonLivraison>()
        .Name("grid-lines-doc")

        // Declare grid column
        .Columns(columns =>
        {
            // Cretae all the columns base on Model
            columns.Bound(l => l.Article).EditorTemplateName("AutoCompleteArticle");
            columns.Bound(l => l.Designation);
            columns.Bound(l => l.Quantite);
            columns.Bound(l => l.Unite);
            columns.Bound(l => l.Commentaire);
            columns.Bound(l => l.ReferenceExterne);
            columns.Bound(l => l.Commentaire2);

            // Edit and Delete button column
            columns.Command(command =>
            {
                command.Edit();
                command.Destroy();
            }).Width(200);
        })
        .Events(ev => ev.DataBound("initMenus").Edit("onEditGrid"))
        .DataSource(datasoure => datasoure.Ajax()
            .Batch(true)
            .Model(model =>
                {
                    //model.Id(l => l.Document);
                    model.Id(l => l.Ligne);
                })
            .Read(read => read.Action("LinesBonLivraison_Read", "Achat"))
            .Create(create => create.Action("LinesBonLivraison_Add", "Achat"))
            .Update(update => update.Action("LinesBonLivraison_Update", "Achat"))
            .Destroy(delete => delete.Action("LinesBonLivraison_Delete", "Achat"))
            .PageSize(10)
        )

        // Add tool bar with Create button
        .ToolBar(toolbar =>
        {
            toolbar.Create();
            toolbar.Save();
        })

        // Set grid editable.
        .Editable(editable => editable.Mode(GridEditMode.InCell).CreateAt(GridInsertRowPosition.Bottom))

        .Scrollable(scr => scr.Height(327))
        .Sortable()
        .Selectable(sel => sel.Mode(GridSelectionMode.Single).Type(GridSelectionType.Row))
        .Navigatable()
        .Pageable(pageable =>
                      {
                          pageable.Refresh(true);
                          pageable.PageSizes(true);
                          pageable.Messages(msg => msg.Empty(null));
                      })
    )
</div>
}
    <script>
function onSelectArticle(e) {
    var dataItem = this.dataItem(e.item.index());
    var url = '@Url.Action("LineBonLivraison_Add", "Achat")';

    $.ajax({
        url: url,
        data: {
            doc: $("#Numero").val(),
            line: e.item.index(),
            article: dataItem.Code
        }, //parameters go here in object literal form
        type: 'GET',
        datatype: 'json',
        success: function (data) {
            if (data == null)
                //document.getElementById('labelx').innerHTML = "null";
            else {

                var grid = $("#grid-lines-doc").data("kendoGrid");
                var select = grid.select();
                var item = grid.dataItem(select); //prob if it a new row item is null
                item.set("Document", data.Document);
                item.set("Ligne", data.Ligne);
                item.set("Article", data.Article);

                //grid.refresh();
            }
        },
        error: function (req, status, error) {
            //document.getElementById('labelx').innerHTML = error;
        }
    });
}

function onAutoComplete() {
    return {
        text: $("#Article").val()
    };
}
<div>
    @(Html.Kendo().AutoComplete()
            .Name("Article")
            .HtmlAttributes(new { style = "width:" + width + ";" })
            .DataTextField("Code")
            .Filter(FilterType.Contains)
            .Enable(enable)
            .DataSource(source =>
            {
                source.Read(read =>
                {
                    read.Action("GetArticles", "Fiche").Data("onAutoComplete");
                })
                .ServerFiltering(true);
            })
            .Events(e =>
            {
                e.Select("onSelectArticle");
            })
    )
    public JsonResult LineBonLivraison_Add(int? doc, int? line, string article)
    {
        Models.Achat.LineBonLivraison l = new Models.Achat.LineBonLivraison()
        {
            Document = doc,
            Article = article,
            //Fournisseur = doc.Nom,
            Ligne = line,
            StyleLigne = "Style1",
            ReferenceExterne = article
        };

        return Json(l, JsonRequestBehavior.AllowGet);
    }