2

MVC 4 で Kendo UI Grid を使用していますが、1 つだけ例外があります。

グリッドに行を追加し、その後行を更新すると、行を更新するのではなく追加してしまいます。

私が言えることは、追加を押すと、プログラムはMVC コントローラーの Create メソッドに入り、追加後に行で Update を押すと Createメソッドもう一度入力するということです。

ただし、起動直後にグリッドの更新ボタンを押すだけでは、そうではないようです。私はこれをすべてAjax呼び出しで行っているため、呼び出し間でページが更新されないことに関係があります。1 つのコマンドを実行してページを更新するだけで、すべてがうまく機能します。また、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; }
}
4

6 に答える 6

0

location.reload() を使用する代わりに、 e.sender.read(); と書くことができます。

于 2013-09-11T12:53:12.367 に答える
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>
于 2013-08-03T07:57:12.770 に答える
-1

に基づいてチェックを入れることができますrecord.Id。存在する場合は更新し、存在しない場合は新しいレコードを作成します。

于 2014-04-16T12:24:51.057 に答える