2

我正在使用 Kendo MVC 控件开发 MVC .Net Framework v4.5 站点。其中一个页面使用绑定到视图模型的 Kendo 网格。When I edit a row, there is a field which uses a dropdownlist control, and when the selection is changed, the remaining fields in the row should be updated to reflect that newly selected item..I am doing this with a JSON call which then返回用于该行的新视图模型。但是,我不知道如何告诉网格使用从该 JSON 调用返回的数据。有任何想法吗?

编辑:

我附上了一些代码。现在的问题是我可以让网格的数据源“刷新”,但是在 UpdateGridSource jscript 函数中,在 grid.dataSource.read() 行中,网格现在退出了编辑模式,如果我去编辑行,数据恢复到原来的样子

(看法)

    @(Html.Kendo().Grid<RedFile.Models.QuickQuoteItemView>()
      .Name("QuoteItems")
      .Columns(c =>
      {
        c.Bound(p => p.Id).Hidden();
        c.Bound(p => p.Description).EditorTemplateName("Combobox");
        c.Bound(p => p.ItemQty);
        c.Bound(p => p.ItemPrice).Format("{0:c}");
        c.Bound(p => p.Total).ClientTemplate("#= kendo.format('{0:c}', Total)   #").FooterHtmlAttributes(new { style = "text-align:right;" }).ClientFooterTemplate("#= kendo.format('{0:c}', sum)#").HtmlAttributes(new { style = "text-align:right;" }).Width(100);
        c.Command(cmd => { cmd.Edit(); cmd.Destroy(); }).Title("Commands").Width(200);
      })
      .ToolBar(tb => tb.Create())
      .ClientDetailTemplateId("subitems")
      .Editable(eb => eb.Mode(GridEditMode.InLine).CreateAt(GridInsertRowPosition.Bottom))
      .Events(e=>e.Edit("editGrid"))
      .Scrollable()
      .DataSource(ds => ds
        .Ajax()
        .ServerOperation(false)
        .Aggregates(a =>
        {
            a.Add(p => p.LineTotal).Sum();
            a.Add(p => p.Total).Sum();

        })
       .Model(model =>
       {
            model.Id(p => p.Id);
            model.Field(p => p.LineTotal).Editable(false);
            model.Field(p => p.ItemPrice).Editable(true);
       })
      .Read(read => read.Action("GridSelect", "QuickQuote", new { qqid = Model.Id })).ServerOperation(false)
      .Create(create => create.Action("GridCreate", "QuickQuote", new { qqid = Model.Id }))
      .Update(update => update.Action("GridUpdate", "QuickQuote"))
      .Destroy(destroy => destroy.Action("GridDelete", "QuickQuote"))
      )
    )

    <script type="text/javascript">

    function dropDownSelectionChanged(e) {
      var dataItem = this.dataItem(e.item.index());
      UpdateGridSource(dataItem.Value);
    }

    function UpdateGridSource(DropDownValue) {
      var grid = $("#theGrid").data("kendoGrid");
      grid.dataSource.transport.options.read.url = '/Controller/refreshGridDataSource?selection=' + DropDownValue;
      grid.dataSource.read();
    }

    </script>

(控制器)

    public JsonResult refreshQuickQuoteTest2([DataSourceRequest] DataSourceRequest request, string selection)
    {
        QuickQuoteItem qqi = db.QuickQuoteItems.FirstOrDefault(p => p.ItemID == selection);

        QuickQuote quickQuoteOriginal = qqi.QuickQuote;

        QuickQuoteItemView qqiv = new QuickQuoteItemView();
        IEnumerable<QuickQuoteItemView> return2;

        qqiv.Id = qqi.Id;
        qqiv.ItemID = qqi.ItemID;
        qqiv.ItemPrice = qqi.ItemPrice;

        if (Session["quickQuote"] != null)
        {
            QuickQuote qq = (QuickQuote)(Session["quickQuote"]);

            if (Session["editingQuickQuoteId"] != null)
            {
                int row = Convert.ToInt32(Session["editingQuickQuoteId"]);
                foreach (var item in qq.QuickQuoteItems)
                {
                    if (item.Id == row)
                    {
                        QuickQuoteItem test = new QuickQuoteItem();
                        StandardItem stdItem = new StandardItem();
                        stdItem = db.StandardItems.Find(selection);
                        item.ItemPrice = stdItem.Price;
                        item.Description = stdItem.Description;
                        item.ItemID = stdItem.ItemId;
                    }
                }
            }                

            return2 = QuickQuoteItemViewItems(qq);
        }
        else
        {
            return2 = QuickQuoteItemViewItems(quickQuoteOriginal);
        }

        return Json(return2.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }
4

1 回答 1

0

您可以使用网格 Ajax 绑定来调用您的控制器更新操作方法

.DataSource(dataSource => dataSource
.Ajax()
.ServerOperation(false)
.Model(model => model.Id(p => p.SampleId))
.Update(read => read.Action("SampleGrid_Update", "Controller")))

然后你需要用DataSourceRequest属性装饰你的方法数据源参数并返回 ModelState 作为DataSourceResult

 [HttpPost]
    public ActionResult SampleGrid_Update([DataSourceRequest] DataSourceRequest dsRequest, IEnumerable<SampleViewModel> viewModel)
    {
        if (viewModel != null && ModelState.IsValid)
        {
            // other code goes here...
        }

        return Json(ModelState.ToDataSourceResult());
    }

您可以在 Kendo网站上查看示例和文档

编辑

您可以将 ajax 调用绑定到下拉列表的更改事件,当它返回时,您可以使用从服务器刷新网格数据

$("#gridId").data("kendoGrid").dataSource.read();
于 2013-06-19T02:34:47.960 回答