【问题标题】:How do I update specific fields from a Kendo Grid that displays related data?如何更新显示相关数据的剑道网格中的特定字段?
【发布时间】:2019-02-06 13:07:26
【问题描述】:

我有一个 KendoUI Grid,它通过我的 SQL 数据库中的视图显示相关数据。它显示的两个表是CarsBookings

表格:汽车

  • 公共 int ID
  • 公共字符串注册
  • 公共字符串制作
  • 公共字符串模型
  • 公共字符串类型
  • 公共字符串燃料

表格:预订

  • 公共 int ID
  • 公共字符串注释
  • public DateTime BookingStart
  • public DateTime BookingEnd
  • 公共 int Car_Id

网格的代码是:

@(Html.Kendo().Grid<MyProject.ViewModels.CarBookings>()
.Name("Bookings")
.Columns(columns => {
    columns.Bound(c => c.Id);
    columns.Bound(c => c.BookingId);
    columns.Bound(c => c.Reg);
    columns.Bound(c => c.Make);
    columns.Bound(c => c.Model);
    columns.Bound(c => c.BookingStart).Format("{0:dd/MM/yyyy}");
    columns.Bound(c => c.BookingEnd).Format("{0:dd/MM/yyyy}");
    columns.Command(command => { command.Edit(); }).Width(250);
})
.Pageable()
.Sortable()
.Groupable()
.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model => {
        model.Id(p => p.Id);
        model.Field(p => p.Id).Editable(false);
        model.Field(p => p.Car_Id).Editable(false);
    })
    .Read(read => read.Action("GetBookings", "Bookings"))
                        .Update(update => update.Action("UpdateBookings", "Bookings", new { BookingId = "#=BookingId#" })))

))

我们允许用户使用网格的内联编辑功能并更新详细信息。我遇到的问题是,当从网格提交更改时,更新方法参数是整个模型(汽车和预订),它将所有不在网格中的字段覆盖为 null,有效地清除了我的数据,但项目除外已更新。

为了解决这个问题,我想我可以使用 [Bind(Include = "")] 并指定我要更新的字段,EF 会保留其余字段,但它不起作用。

这是UpdateBooking 方法,它非常粗略地尝试更新两个表并指定我要更新的字段。

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult UpdateBookings([DataSourceRequest] DataSourceRequest request, [Bind(Include = "Reg, Make, Model")]  Cars car, [Bind(Include = "BookingStart, BookingEnd")] Bookings booking, int BookingId, BookingsViewModel vm)
    {
        if (ModelState.IsValid)
        {
            unitOfWork.CarRepository.Update(car);                
            booking.Id = BookingId;
            unitOfWork.BookingRepository.Update(booking);
            unitOfWork.Save();
        }

        return Json(new[] { vm }.ToDataSourceResult(request, ModelState));
    }

当您尝试更新时,该参数会传递汽车和预订的整个模型,并将不在网格中的字段设置为 null,这会清除现有数据。有什么方法可以告诉我的方法只更新从网格传入的字段并保持其余数据不变?

【问题讨论】:

  • 一般来说,我只会使用视图模型(不要传递实体)。因此,现在在您的更新方法中获取汽车和预订,然后替换更新的字段(自动映射器非常适合此)。否则,您将需要在网格定义中包含所有汽车和预订字段(您可以将它们隐藏起来)。
  • 感谢@SteveGreene,我担心必须带回所有记录并隐藏它们。你能给我一个使用视图模型的例子吗?非常感谢

标签: entity-framework model-view-controller kendo-ui


【解决方案1】:

这就是我们所做的。

public JsonResult Update([DataSourceRequest] DataSourceRequest request, MyViewModel myViewModel)
{
    if (ModelState.IsValid)
    {
        // Get 1st entity to update
        var myEntity = _db.MyEntities.Single(s => s.MyID == myViewModel.MyID);

        // Automapper will copy the viewmodel fields to the entity (could do manually as well)
        Mapper.Map(myViewModel, myEntity);

        // Repeat for 2nd entity...

        _db.SaveChanges();

        // May need to refresh viewmodel if other fields were affected by insert/update
    }

    return Json(new[] { myViewModel }.AsQueryable().ToDataSourceResult(request, ModelState));
}

【讨论】:

  • 感谢您的回答,我现在收到一个新错误:属性“Id”是对象关键信息的一部分,无法修改。 '
  • 在 SaveChanges() 之前检查每个实体的 Id 值。对于更新,它应该是非零的。可能是将视图模型映射回实体的问题。使用当前代码更新您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
相关资源
最近更新 更多