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

Grid batch updating with Web API

3 Answers 260 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Donna Stewart
Top achievements
Rank 1
Donna Stewart asked on 12 Nov 2014, 10:19 PM
I have a grid in which I would like to use batch editing.  However, the update is not working - I am seeing no data when I hit the Web API controller.  I have read the Web API editing document on the Kendo site and I have looked at the example application in the download of UI for ASP.NET MVC.  I have Googled and searched these forums. I can find no example of batch editing using Web API without OData.  I am not using OData.  If anyone can be of assistance, I would be so very appreciative!  I can get inline editing to work, no problem.  But I would really like to use batch editing.
I have attached a couple of screen shots showing the empty parameter on my controller method and the request header and data from the Web API call.

Here is my code:

Grid:
@(Html.Kendo().Grid<GMCWebApplication.Areas.admin.Models.UserGridModel>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(c => c.Id).Hidden();
        columns.Bound(c => c.UserFName).Title("First Name");
        columns.Bound(c => c.UserLName).Title("Last Name");
        columns.Bound(c => c.UserName);
        columns.Bound(c => c.PasswordHash).Hidden();
        columns.Bound(c => c.Email);
        columns.Template(@<text></text>).ClientTemplate("<input type='checkbox' #= IsBIUser ? checked='checked':'' # class='chkbx' />").Width(100).Title("BI User");
        columns.Bound(c => c.StartDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(c => c.EndDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(c => c.UserKey).Hidden();
    })
    .ToolBar(toolbar => toolbar.Save())
    .Editable(ed => ed.Mode(GridEditMode.InCell))
    .Sortable()
    .Pageable(pageable => pageable
        .Refresh(true)
        .PageSizes(true)
        .ButtonCount(5))
    .Filterable()
    .DataSource(dataSource => dataSource
        .WebApi()
        .Batch(true)
        .Model(model => model.Id(c => c.Id))
        .Read(read => read.Url("/api/gmcmembership/au/11712").Type(HttpVerbs.Get))
        .Update(update => update.Url("/api/gmcmembership/uu").Type(HttpVerbs.Put))
    )
)

WebAPI Controller code for update:
/// <summary>
/// Updates user(s) in GMCMembership System with changes made in Kendo Grid
/// </summary>
/// <param name="users"></param>
/// <remarks> </remarks>
[System.Web.Http.HttpPut]
[System.Web.Http.Route("uu")]
public HttpResponseMessage UpdateUsers([Bind(Prefix = "models")]IEnumerable<UserGridModel> users )
{
    try
    {
        using (
            var um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))
            )
        {
            foreach (var user in users)
            {
                var id = user.Id;
                var userToUpdate = new ApplicationUser();
                userToUpdate = um.Users.SingleOrDefault(u => u.Id == id);
                if (userToUpdate != null)
                {
                    userToUpdate.Email = user.Email;
                    userToUpdate.EndDate = (DateTime)user.EndDate;
                    userToUpdate.IsBIUser = user.IsBIUser;
                    userToUpdate.StartDate = (DateTime)user.StartDate;
                    userToUpdate.UserFName = user.UserFName;
                    userToUpdate.UserLName = user.UserLName;
                    userToUpdate.UserName = user.UserName;
 
                    um.Update(userToUpdate);
                }
            }
        }
        return Request.CreateResponse(HttpStatusCode.OK);
    }
    catch (Exception ex)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
        return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
    }
}

Thanks!
Donna

3 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 14 Nov 2014, 04:50 PM
Hi Donna,

We are not sure if Web API supports batch updates. For example the signature for updating a record is something like

public HttpResponseMessage Put(int id, Model model)
{
}

We don't know if it is possible to make it accept a collection of models.

Still you can use InCell edit mode with Web API but you have to disable batch updates. The grid will make separate HTTP requests for every change and work as expected.

Regards,
Atanas Korchev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Donna Stewart
Top achievements
Rank 1
answered on 14 Nov 2014, 04:57 PM
Hi Atanas,

Thank you for your response.  I guess I didn't realize you could do InCell editing with batch mode turned off.  I have already changed my code to use the MVC controller for my grid, so all works great now.  If I have time, I may go back and change to use the InCell  editing with batch mode disabled and try with the Web Api controller again.

Thanks again for your help,
Donna
0
Robert
Top achievements
Rank 1
answered on 03 Nov 2015, 02:11 PM
Is this still the case?  I am converting to WebApi and I have several grids that use Batch editing.  
Tags
Grid
Asked by
Donna Stewart
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Donna Stewart
Top achievements
Rank 1
Robert
Top achievements
Rank 1
Share this question
or