Kendo MVC 在 Update 调用上触发 Create 方法

use*_*784 2 c# kendo-grid kendo-asp.net-mvc

我正在将 Kendo UI Grid 与 MVC 4 一起使用,它运行良好,只有一个例外。

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

我能说的是,如果我按添加,程序会在我的 MVC 控制器中输入Create方法,如果我在添加的一行上按更新,我将再次进入Create方法。

但是,如果我在启动后直接按下网格中的更新按钮,情况似乎并非如此。我正在使用 Ajax 调用完成所有这些操作,因此它与在调用之间不更新的页面有关。如果我只执行一个命令然后刷新页面,一切都会很好。我也使用实体框架作为 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"))
  ))
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

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());
    } 
}
Run Code Online (Sandbox Code Playgroud)

这是我的视图模型

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; }
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*can 5

我记得以前有过这个问题,但我找不到我是如何解决它的。

将模型保存到数据库后,在从方法中返回它之前,请确保它具有它的 ID。(调试并观察model对象)。如果没有,您可以尝试以下操作:

model.Id = saveditem.Id;
return Json(new[] { model }.ToDataSourceResult(request, ModelState));
Run Code Online (Sandbox Code Playgroud)