更新调用上的Kendo MVC fire Create方法

时间:2013-08-01 15:29:21

标签: c# kendo-grid kendo-asp.net-mvc

我正在使用带有MVC 4的Kendo UI Grid,它只有一个例外,效果很好。

如果我在网格中添加一行,然后更新一行,它最终会添加一行而不是更新它。

我能说的是,如果我按下add,程序会在我的MVC控制器中输入 Create 方法,如果我在后按>添加I再次进入创建方法。

但是,如果我在启动后直接按下网格中的更新按钮,情况似乎并非如此。我正在使用Ajax调用完成所有这些,因此它与页面之间没有更新调用之间的关系。如果我只做一个命令然后刷新页面,一切都很好。我也使用Entity Framework作为ORM。

这是我的剃刀观点:

@model IEnumerable<Internal.License.Management.Web.UI.Models.PriceListViewModel>

@{
   ViewBag.Title = "Price List";
}

@(Html.Kendo().Grid<Internal.License.Management.Web.UI.Models.PriceListViewModel>()
.Name("grid")
.Columns(columns =>
    {
        columns.Bound(p => p.Name).Width(120);
        columns.Bound(p => p.Code).Width(180);
        columns.Bound(p => p.Interval).Width(180);
        columns.Bound(p => p.PricePerUnit).Width(200);
        columns.Bound(p => p.Currency).Width(120);
        columns.Command(commands =>
            {
                commands.Edit();
                commands.Destroy();
            }).Width(172);
    })
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource =>
    dataSource.Ajax()
    .PageSize(20)
    .Events(events => events.Error("error_handler"))
    .Model(model => model.Id(p => p.Id))
    .Create(create => create.Action("Create", "PriceList"))
        .Read(read => read.Action("Read", "PriceList"))
        .Update(update => update.Action("Update", "PriceList"))
        .Destroy(destroy => destroy.Action("Delete", "PriceList"))
  ))

这是我的控制器:

public class PriceListController : Controller
{
    public ActionResult List()
    {
        return View("PriceList");
    }

    public ActionResult Read([DataSourceRequest] DataSourceRequest request)
    {
        var model = new DataAdapter().GetAllOptionTemplates();
        var result = model.ToDataSourceResult(request);

        return Json(result);
    }

    [HttpPost]
    public ActionResult Create([DataSourceRequest] DataSourceRequest request,  
                           PriceListViewModel model)
    {
        if (model != null && ModelState.IsValid)
        {
           new DataAdapter().SaveToDatabase(model);
        }

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

    [HttpPost]
    public ActionResult Update([DataSourceRequest] DataSourceRequest request,   
                           PriceListViewModel model)
    {
        if (model != null && ModelState.IsValid)
        {
           new DataAdapter().UpdateToDatabase(model);
        }

        return Json(ModelState.ToDataSourceResult());
    } 
}

这是我的观点型号

public class PriceListViewModel
{
    public int Id { get; set; }
    public string Currency { get; set; }
    [DisplayName("Option Name")]
    public string Name { get; set; }
    public string Code { get; set; }
    public string Interval { get; set; }
    public decimal PricePerUnit { get; set; }
}

6 个答案:

答案 0 :(得分:4)

我记得曾经有过这个问题,但我找不到如何修复它。

将模型保存到数据库后,在从方法返回之前确保它具有ID。 (调试并观察model对象)。如果没有,你可以尝试这样的事情:

model.Id = saveditem.Id;
return Json(new[] { model }.ToDataSourceResult(request, ModelState));

答案 1 :(得分:0)

我没有找到这个问题的原因,但我写了一个解决方法。

我更改了KendoUI代码部分:

.Events(events => events.Error("error_handler"))

为:

.Events(events => 
            { 
               events.Error("error_handler");
               events.RequestEnd("force_update");
            })

然后在我显示网格的页面中添加了一个javascript。

<script type="text/javascript">
    function force_update(e) {
        if (e.type === "create") {
            location.reload();
        }
     }
</script>

答案 2 :(得分:0)

使用location.reload()可以编写e.sender.read();

答案 3 :(得分:0)

对我来说,问题在于网格中存在现有冲突,因为我没有正确清空数据源

确保像这样清空数据源:

$(“#grid”)。data(“ kendoGrid”)。dataSource.data([]);

答案 4 :(得分:-1)

您可以根据record.Id进行检查。如果存在,则更新,如果不存在则再创建新记录。

答案 5 :(得分:-1)

我也遇到了这个问题。事实是,您的网格没有通过 Read 方法获取记录的ID,因此它调用了Create而不是Update