Kendo : ComboBox in a grid - Sending additional da

2019-01-29 00:07发布

ASP.NET MVC5

I have a combobox in a grid (InLine Edit):

columns.Bound(x=>x.AccountID).EditorTemplateName("MyTemplate")

Where MyTemplate is in /Shared

There are millions of Accounts.

When I try to edit the combo box in a grid and choose a new value, the ID of the Account, not the name, appears. This is because of course the name of the account is not immediately present so in the Read().Data() of the ComboBox.Datasource I need to send additional data; the AccountID.

My ComboBox Template looks like this:

.DataSource(source=>
   source.Read(read =>
      read.Action("ReadAccounts".....)
         .Data("HERE IS WHERE I NEED TO SEND THE ACCOUNT ID AS EXTRA DATA 
             WHEN THIS CBO TEMPLATE IS IN A GRID")

Thank you

1条回答
一夜七次
2楼-- · 2019-01-29 00:50

Here's the Combo Box defined in a partial view in ~/Views/Shared/EditorTemplates/ComboBoxTemplate

@(Html.Kendo().ComboBox()
          .Name("AcctName")//must match Field Name that is being edited
          .HtmlAttributes(new { style = "width:250px" })
          .DataTextField("AcctName")
          .DataValueField("AcctCd")
          .Filter(FilterType.StartsWith)
          .AutoBind(true)
          .MinLength(3)
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetCombo", "GridPost").Data("OnAdditionalData");
              })
              .ServerFiltering(true);
          })          
)

Here's the view and controller action

columns.Bound(x => x.AcctName).Title("Acct Name").EditorTemplateName("ComboBoxTemplate");

 function OnAdditionalData() {          

            var entityGrid = $("#ProposalGridX").data("kendoGrid");
            var selected = entityGrid.dataItem(entityGrid.select());
            //if the id is off the Grid Row and not the ComboBox
            //select the row and pull the fields
            //selected.PropertyName

            return {
                text : $("#AcctName").data("kendoComboBox").text(),
                 val : $("#AcctName").data("kendoComboBox").value()
            };
        }

   public JsonResult GetCombo(string text, string val)
   {
         List<PortfolioViewModel> model = new AUMBusiness().GetAum(DateTime.Now);

           if (!string.IsNullOrEmpty(text))
            {
               model = model.Where(x => x.AcctName.StartsWith(text)).ToList();
            }

         return Json(model, JsonRequestBehavior.AllowGet);
    }

Like with any Ajax calls, placing break points in the code might prevent the widget from performing as intended. For ex. using incell editing while clicking the Field to edit, if you place a breakpoint in GetCombo the ComboBox editor template will not default correctly to that value.

查看更多
登录 后发表回答