This is a migrated thread and some comments may be shown as answers.

How to add rows using MVC model

1 Answer 284 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 17 Sep 2012, 10:33 PM
I have a models
Route:
int RouteID;
String RouteName;
IEnumerable<RouteCustomer> Customers;

RouteCustomer:
int RouteID;
int CustomerID;
int Sun;
int Mon;
int Tue;
int Wed;
int Thue;
int Fri;
int Sat;

Customer:
int CustomerID;
String Name;

I have created an edit form that allows changes (route name) and a grid for the customer information.  What is happening now is that I am not getting the events I need when adding a new row to the grid:

Here is the grid code:
@(Html.Kendo().Grid<LightRouteDB.RouteCustomer>()
            .Name("CustomerGrid")
            .Columns(cols =>
                {
                    cols.Bound(r => r.CustomerID);
                    //cols.Bound(r => r.Customer.CustomerName);
                    cols.Bound(r => r.Sun);
                    cols.Bound(r => r.Mon);
                    cols.Bound(r => r.Tue);
                    cols.Bound(r => r.Wed);
                    cols.Bound(r => r.Thu);
                    cols.Bound(r => r.Fri);
                    cols.Bound(r => r.Sat);
                    cols.Command(command => { command.Edit(); command.Destroy(); }).Width(200);
                })
                .ToolBar(toolbar => toolbar.Create())
                .Editable(editable => editable.Mode(GridEditMode.InLine))
                .Pageable()
                .Sortable()
                .Scrollable()
                .DataSource(ds => ds
                    .Ajax()
                    .Events(events => events.Error("error_handler"))
                        .Model(m => { m.Id(p => p.RouteCustomerID); m.Field(f => f.RouteID).DefaultValue(Model.Route.RouteID); })
                    .Create(a => a.Action("CreateCustomerRow", "Routes"))
                    .Read(r => r.Action("ReadCustomerRows", "Routes"))
                    .Update(u => u.Action("UpdateCustomerRow", "Routes"))
                    .Destroy(d => d.Action("DestroyCustomerRow", "Routes"))
                )
          )
And here is the controller:
public ActionResult ReadCustomerRows(int id, [DataSourceRequest] DataSourceRequest request)
       {
           return Json(getRouteCustomersFromSession().ToDataSourceResult(request));
       }
 
       [AcceptVerbs(HttpVerbs.Post)]
       public ActionResult CreateCustomerRow(int id, [DataSourceRequest] DataSourceRequest request,  RouteCustomer customer)
       {
           if (customer != null && ModelState.IsValid)
           {
               IList<RouteCustomer> rc = getRouteCustomersFromSession();
               rc.Add(customer);
           }
 
           return Json(new[] { customer }.ToDataSourceResult(request, ModelState));
       }
 
       [AcceptVerbs(HttpVerbs.Post)]
       public ActionResult UpdateCustomerRow([DataSourceRequest] DataSourceRequest request, RouteCustomer customer)
       {
           if (customer != null && ModelState.IsValid)
           {
               IList<RouteCustomer> rc = getRouteCustomersFromSession();
               var target = rc.Where(c => c.RouteCustomerID == customer.CustomerID).First();
               if (target != null)
               {
                   target.CustomerID = customer.CustomerID;
                   saveRouteCustomersToSession(rc);
               }
           }
 
           return Json(ModelState.ToDataSourceResult());
       }

The events in the controller just don't seem to fire.  I thought I copied it from the sample programs, but it doesn't seem to work correctly.  Any help would be appreciated.

1 Answer, 1 is accepted

Sort by
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 02:31 PM
Solved it.  Bad controller definition (Don't need to pass the ID).
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
John
Top achievements
Rank 1
Share this question
or