Kendo Grid: Foreign Key Dropdown does not update grid cell after update

Posted by JookyDFW on Stack Overflow See other posts from Stack Overflow or by JookyDFW
Published on 2013-11-01T15:27:39Z Indexed on 2013/11/04 15:54 UTC
Read the original article Hit count: 2570

Filed under:
|
|
|
|

I have a Kendo MVC grid that contains a nullable property (short) that is bound as a foreign key and uses a dropdown list as an editor template. I am also using inline editing.

When the property value is null, the dropdown list selected value does not get set into the grid cell after the update button is clicked. This works fine if incell editing is used. I am looking for a workaround that will solve my problem. I am including a stripped down version of my code below

Everything works if the nullable value is set to a non-null value.

GRID

@(Html.Kendo().Grid<AssetViewModel>()
   .Name("DealAssets")
   .Columns(c =>
   {
      c.Bound(x => x.Name);
      c.ForeignKey(x => x.AssetTypeID, (IEnumerable<SelectListItem>)ViewBag.AssetTypeList, "Value", "Text");
      c.ForeignKey(x => x.SeniorityTypeID, seniorityTypeList, "Value", "Text").EditorTemplateName("GridNullableForeignKey");
      c.ForeignKey(x => x.RateBaseID, rateBaseList, "Value", "Text").EditorTemplateName("GridNullableForeignKey"); ;
      c.Command(m => { m.Edit(); m.Destroy(); });
   })
   .ToolBar(toolbar => toolbar.Create().Text("Add New Asset"))
   .Editable(x => x.Mode(GridEditMode.InLine))
   .DataSource(ds => ds
      .Ajax()
      .Model(model => model.Id(request => request.ID))
      .Read(read => read.Action("ReadAssets", "Deal", new { id = Model.ID }))
      .Create(create => create.Action("CreateAsset", "Deal", new { currentDealID = Model.ID }))
      .Update(update => update.Action("UpdateAsset", "Deal"))
      .Destroy(destroy => destroy.Action("DeleteAsset", "Deal"))
   )
)

EDITOR TEMPLATE

@model short?
@{
   var controlName = ViewData.TemplateInfo.GetFullHtmlFieldName("");
}
@(
   Html.Kendo().DropDownListFor(m => m)
      .Name(controlName)
      .OptionLabel("- Please select -")
      .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)

UPDATE ACTION

public ActionResult UpdateAsset([DataSourceRequest] DataSourceRequest request, int ID)
{
   var dealAsset = DataContext.DealAssets.SingleOrDefault(o => o.ID == ID);
   if (dealAsset != null)
   {
      if (TryUpdateModel(dealAsset.Asset, new[] {"Name","AssetTypeID","SeniorityTypeID","RateBaseID" }))
      {
         DataContext.SaveChanges();
      }
   }
   return Json(new[] { new AssetViewModel(dealAsset) }.ToDataSourceResult(request, ModelState), JsonRequestBehavior.AllowGet);
}

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about mvc