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

How to modify an existing record in the telerik grid?

4 Answers 67 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Victor
Top achievements
Rank 1
Victor asked on 08 May 2015, 04:25 PM

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?

4 Answers, 1 is accepted

Sort by
0
Accepted
Joshua
Top achievements
Rank 2
answered on 08 May 2015, 04:36 PM

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>

0
Victor
Top achievements
Rank 1
answered on 08 May 2015, 04:59 PM

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

This fixed the issue. thanks. 

0
Joshua
Top achievements
Rank 2
answered on 08 May 2015, 05:01 PM

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.

0
Dimiter Madjarov
Telerik team
answered on 12 May 2015, 07:50 AM

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.

 
Tags
Grid
Asked by
Victor
Top achievements
Rank 1
Answers by
Joshua
Top achievements
Rank 2
Victor
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Share this question
or