Kendo Grid Foreign key column bind dynamically

北城余情 提交于 2019-11-30 10:19:08

Kendo grid does not support dynamic rebinding. The closest thing you can get is to define a custom Editor Template which binds data using AJAX.

columns.ForeignKey(p => p.CPOID, 
(System.Collections.IEnumerable)ViewData["CPOs"], "cpo_id", "contract_po")
.Title("Company - Contact/Purchase Order")
.EditorTemplateName("RemoteForeignKey");

RemoteForeignKey Editor Template

@model int

@(Html.Kendo().DropDownListFor(m => m)
  .DataSource(source =>
  {
      source.Read(read =>
      {
          read.Action("actionName", "controllerName").Type(HttpVerbs.Post).Data("dataFunc");
      }).ServerFiltering(false);
  })
  .DataValueField("cpo_id")
  .DataTextField("contract_po")
)

dataFunc javascript function

function dataFunc () {
    return {
        SiteID: $("#SiteID").val() // here we pass the site ID to server
    };
}

and your server function

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult actionName(int? SiteID)
{
    if (SiteID != null)
    {
        var objects = (from obj in db.tableName
                       where obj.SiteID == SiteID.Value
                       select new
                       {
                           cpo_id = obj.cpo_id,
                           contract_po = obj.contract_po
                       }).ToList().Distinct().OrderBy(obj => obj.contract_po);
            return Json(objects);
    }
    return null;
}

Then the values in your dropdown will be filtered by the current value of SiteID input.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!