4

我有一个 Kendo MVC 网格,其中包含一个可空属性(短),该属性绑定为外键并使用下拉列表作为编辑器模板。我也在使用内联编辑。

当属性值为 null 时,单击更新按钮后,下拉列表选择值不会设置到网格单元格中。如果使用 incell 编辑,这可以正常工作。我正在寻找可以解决我的问题的解决方法。我在下面包含了我的代码的精简版本

如果可空值设置为非空值,则一切正常。

网格

@(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"))
   )
)

编辑器模板

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

更新操作

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);
}
4

1 回答 1

12

Telerik 最近刚刚在他们的选择列表中添加了一个新的 HTML 属性 data_value_primitive,以解决上述问题。新属性应添加到外键编辑器模板中并设置为 true。

 Html.Kendo().DropDownListFor(m => m)
        .Name(controlName)
        .OptionLabel("- Please select -")
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
        **.HtmlAttributes(new { data_value_primitive = true})**

最后一部分是对 update 方法的修改,以说明在执行 ajax 调用时网格不传回 null 属性。我认为这与 TryUpdateModel 方法的工作方式有关

...
if (TryUpdateModel(dealAsset.Asset, new[] {"Name","AssetTypeID","SeniorityTypeID","RateBaseID" }))
{
   // If no property passed back then set it to null
   var senorityTypeID = ValueProvider.GetValue("SeniorityTypeID");
   if (senorityTypeID == null)
   {
      dealAsset.Asset.SeniorityTypeID = null;
   } else {
      dealAsset.Asset.SeniorityTypeID = (short)senorityTypeID.ConvertTo(typeof(short));
   }
   var rateBaseID = ValueProvider.GetValue("RateBaseID");
   if (rateBaseID == null)
   {
      dealAsset.Asset.RateBaseID = null;
   } else {
      dealAsset.Asset.RateBaseID = (byte)rateBaseID.ConvertTo(typeof(byte));
   }
   DataContext.SaveChanges();
}
'''
于 2013-11-01T15:27:39.047 回答