当数据绑定细节网格时,使用MVC包装器的Kendo UI的最佳实践



我想知道如何在将kendo ui与MVC一起使用时如何最好地构建细节网格视图/控制器。我的详细模型看起来像这样:

public class EditablePinNote
{
    [ScaffoldColumn(false)]
    public int PinId { get; set; }
    public int NoteId { get; set; }
    [Editable(false)]
    public int? SaleYear { get; set; }
    public string Note { get; set; }
}

查看

      @(Html.Kendo().Grid<W2D.Models.EditablePinNote>()
        .Name("PinNote_#=Id#")
        .ToolBar(commands => commands.Create().Text("Add Note"))
        .Editable(editing => editing.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
        .Scrollable()
        .DataSource(dataBinding => dataBinding
            .Ajax()
            .Create(c=> c.Action("Create","PinNote", new { pPinId = "#=PinId#" , pSaleYear="#=TaxYear#"}))
            .Read("Read", "PinNote", new { PinId = "#=PinId#" })
            .Update("Update", "PinNote")
            .Destroy("Destroy","PinNote")
            .Model(model => 
            {
                model.Id(p => p.NoteId);
            })
            .Events(events => events.Error("error_handler"))
        )
        .Columns(columns =>
            {
                columns.Bound(c => c.SaleYear).Width(80).Title("Year");
                columns.Bound(c => c.Note);
                columns.Command(command =>
                {
                    command.Edit().CancelText(" ").Text(" ").UpdateText(" ").HtmlAttributes(new { style = "min-width: 10px" });
                    if (User.IsInRole("admin")) command.Destroy().Text(" ").HtmlAttributes(new { style = "min-width: 10px" });
                }).Width(80);
            })
         .ToClientTemplate()
    )

和用于创建操作的控制器

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create([DataSourceRequest]DataSourceRequest request, EditablePinNote model, int pPinId, int? pSaleYear)
    {
        model.PinId = pPinId;
        model.SaleYear = pSaleYear;
        if (TryValidateModel(model)) //TryUpdateModel will overwrite set values
        {
            StorePinNote.Insert(model);
        }
        return Json(new[] { model }.ToDataSourceResult(request, ModelState));
    }

这项工作似乎应该有更好的方法。我不喜欢的部分是控制器。需要传递参数ppinid和psaleyear,因为没有办法(至少我不知道如何在视图中将它们添加到模型中)。另外,如果调用与模型中字段名称相同的名称的参数,则模型绑定将无效,因此人们需要将它们称为"模型绑定到工作"的不同事物。总而言之,我想在视图中设置PINID和Saleyear的模型值,然后控制器将具有一个不错的签名,例如CREATE([DATASOURCERECERQUEST] DATASOURCERCERQUEST请求,Editable Pinnote模型)。如果一个人可以在详细信息模型中以某种方式设置构造函数或以某种方式设置详细信息代码的字段值。

您是否尝试过?:

        .Model(model => 
        {
            model.Id(p => p.NoteId);
            model.Field(p => p.PinId).DefaultValue("#=PinId#");
        })

我在自己的应用程序中做了类似的事情。

telerik确实允许传递参数。

此示例具有硬编码的值,但是您可以使用GetElementByID从视图中的字段中获取值,并用作参数的值而不是硬编码值。

@(Html.Kendo().Grid<RoutingInquiryModel>()
   .Name("RoutingInquirys")
  .Columns(c =>
  {
      c.Bound(p => p.TextValue);
      c.Bound(p => p.DataValue);
      c.Bound(p => p.RoutingDescription);
      c.Bound(p => p.RoutingRev);
  })
  .DataSource(d => d
                .Ajax()
                .Read(read => read.Action("get", "RoutingInquirys",
                                          new { pPlant = "PL2" } ) )
                .PageSize(12)
             )
    .Pageable()
    .Filterable()
    .Selectable()
    .Sortable()
    .Groupable()
    .Scrollable()
    .Resizable(resize => resize.Columns(true))
)

在控制器中,代码将类似于

public ActionResult Get([DataSourceRequest] DataSourceRequest request, String pPlant)
{ 
    ...

pPlant是您在控制器中使用的参数的名称。对于超过1个参数,它们是分离的。

最新更新