Sanju Rao
Sanju Rao

Reputation: 331

How to send values to a method on kendo grid edit click event

I have kendo gird and populating the grid as below code. However When I click on Edit it displays my primary key value as well in edit pop up. After fixing this issue i need to pass 2 parameters to my action method to save. Can any one please help me to fix this?

My .cshtml code

  @(Html.Kendo().Grid<abcFinal.Models.AdminCatgMgmt.catlistitems>()
                .Name("mainCategoryGrid")
                .Columns(columns =>
                {
                    columns.Bound(p => p.catid).Width(100).Title("ID");
                    columns.Bound(p => p.catname).Width(100).Title("Category Name");
                    columns.Bound(p => p.noofsubcat).Width(100).Title("No. of Sub Category");
                    columns.Bound(p => p.catdate).Width(100).Title("Category Date");
                    columns.Command(command => { command.Edit(); command.Destroy(); }).Width(160).Title("Modify");
                })
                .ToolBar(toolbar => toolbar.Create())
                .Editable(editable => editable.Mode(GridEditMode.PopUp))
                .Pageable()
                .Sortable()
                .Scrollable()
                .HtmlAttributes(new { style = "height:500px;" })
                .DataSource(dataSource => dataSource
                            .Ajax()
                            .PageSize(10)
                            .Events(events => events.Error("error_handler"))
                            .Model(model => model.Id(p => p.catid))
                            .Create(update => update.Action("AddMainCategory", "AdminOrders"))
                            .Read(read => read.Action("getMainCategory", "AdminOrders"))
                            .Update(update => update.Action("EditingPopup_Update", "AdminOrders"))
                            .Destroy(update => update.Action("EditingPopup_Destroy", "AdminOrders")))  

My Controllers action method

        [AcceptVerbs(HttpVerbs.Post)]
        public void AddMainCategory(string id, string status)
        {
        }

Upvotes: 0

Views: 2360

Answers (1)

Jonathan Buchanan
Jonathan Buchanan

Reputation: 878

This is an example form kendo. (http://demos.telerik.com/aspnet-mvc/grid/editing-popup)

Kendo works well with MVVM. You can define a View Model in your project and use that as the second parameter in your create CRUD function. As long as the fields are name the same it should mapped correctly.

Also, as a part of the request of CRUD functions you should have the [DataSourceRequest] DataSourceRequest request. This should always be the first argument of your Kendo CRUD functions.

When you return you should also always return with the .ToDataSourceResult(request, Model) as it will convert your returned data to the Kendo grid in the proper format so that the Grid can be updated with your new row you just added. If you don't do this you will need to do another read of the grid datasource to get the item (either with JQuery or with a page refresh).

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, ProductViewModel product)
{
  if (product != null && ModelState.IsValid)
  {
    productService.Create(product);
  }

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

Refer the Kendo demos for more examples.

Upvotes: 2

Related Questions