How to modify an existing record in the telerik grid?

5 posts, 1 answers
  1. Victor
    Victor avatar
    24 posts
    Member since:
    Feb 2015

    Posted 08 May 2015 Link to this post

    It is probably easy as you might say it is. But the fact remains is that I can not find the solution anywhere.

     Here is my code:

    public ActionResult Edit([DataSourceRequest] DataSourceRequest gridRequest, Sub editSub)
            {
                if (ModelState.IsValid) 
                {
                    Sub subToUpdate = db.Subs.FirstOrDefault();
                    subToUpdate.SFirst = editSub.SFirst;
                    subToUpdate.SLast = editSub.SLast;
                    subToUpdate.SEmail = editSub.SEmail;
                    db.SaveChanges();
                }

                return Json(new[] { editSub }.ToDataSourceResult(gridRequest, ModelState));
            }

    Every time I modify a different row the first row gets overwritten. I assume that I nee to specify the ID in here: db.Subs.FirstOrDefault(); 

    I cannot find how to?

  2. Answer
    Joshua
    Joshua avatar
    12 posts
    Member since:
    Mar 2015

    Posted 08 May 2015 in reply to Victor Link to this post

    Can you show us your front end? (If you need clarification)

     You probably need to tell what the model is so that when the edit goes to the ActionResult it know the correct record to modify. So in my example below look at the dataSource.model part.

    Then on your Edit method in your original post some something like this...

    Sub subToUpdate = db.Subs.FirstOrDefault(s=>s.ID);

    For example....

    @(Html.Kendo().Grid<DepartmentViewModel>()
              .Name("Dept_Grid")
              .Columns(columns =>
              {
                  columns.Bound(p => p.Name).Title("Department");
                  columns.Bound(p => p.AdGuid).Title("AD Identifier");
                  columns.Bound(p => p.ManagerId).Title("Manager");
                  columns.Bound(p => p.DeptEmail).Title("Dept Email");
                  columns.Bound(p => p.IsPublic).Title("Public");
                  columns.Command(command => { command.Edit(); command.Destroy(); }).Width(190);
              })
              .Editable(editable => editable.Mode(GridEditMode.InLine))
              .Sortable()
              .DataSource(dataSource => dataSource
                  .Ajax()
                  .PageSize(20)
                  .Events(events => events.Error("error_handler"))
                  .Model(model => model.Id(p => p.Id))
                  .Read(read => read.Action("DepartmentInline_Read", "Settings"))
                  .Update(update => update.Action("DepartmentInline_Update", "Settings"))
                  .Destroy(update => update.Action("DepartmentInline_Destroy", "Settings"))
              )
              )
        <script type="text/javascript">
            function error_handler(e) {
                if (e.errors) {
                    var message = "Errors:\n";
                    $.each(e.errors, function (key, value) {
                        if ('errors' in value) {
                            $.each(value.errors, function () {
                                message += this + "\n";
                            });
                        }
                    });
                    alert(message);
                }
            }
        </script>

  3. Victor
    Victor avatar
    24 posts
    Member since:
    Feb 2015

    Posted 08 May 2015 in reply to Joshua Link to this post

    Sub subToUpdate = db.Subs.FirstOrDefault(s=>s.ID == editSub.ID);

    This fixed the issue. thanks. 

  4. Joshua
    Joshua avatar
    12 posts
    Member since:
    Mar 2015

    Posted 08 May 2015 in reply to Victor Link to this post

    Oops. Yeah you are right. I rushed my answer and left out the comparison logic.

     Glad it's working for you now. Good luck on your project.

  5. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2304 posts

    Posted 12 May 2015 Link to this post

    Hello guys,

    I am glad the issue is resolved. Have a great day!

    Regards,
    Dimiter Madjarov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top