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

[Solved] Id value sent to Delete action on Server binding

7 Answers 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pål
Top achievements
Rank 1
Pål asked on 19 Jan 2011, 12:04 PM
Hi,

Can someone please help me. I am doing something wrong when using the Grid control. I am using MVC 2 and Q3.
I have a View strongly typed to a Model (Customer). This Customer has a list of CustomerEvents. The View displays information on the Customer and in addition has a Grid of CustomerEvent object. The Grid should support Insert, Edit and Delete.

Here is the View
<%: Html.Telerik().Grid<CustomerEvent>(Model.CustomerEvents)
    .Name("CustomerEvents")
    .DataKeys(keys => keys.Add(ce => ce.ID))
    .ToolBar(commands => commands.Insert())
    .DataBinding(databinding => databinding.Server()
             .Select("Details""Customer"new {id = Model.ID})
             .Insert("Create""CustomerEvent"new { id = Model.ID })
             .Update("Save""CustomerEvent")
             .Delete("Delete""CustomerEvent"))
    .Columns(columns =>
            {
             columns.Bound(p => p.Created).Width(115).Format("{0:d}");
             columns.Bound(p => p.Action).Width(130).Title("Type");
             columns.Bound(p => p.Comment).Width(150).Title("Kommentar");
             columns.Bound(p => p.Officer.FullName).Width(130).Title("RÃ¥dgiver");
             columns.Command(commands =>
                            {
                             commands.Edit();
                             commands.Delete();
                            }).Width(180).Title("");
            })
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Pageable(paging => paging.PageSize(5))
    .Sortable()
    .Filterable()
%>
I have managed to get Insert to work with this code, but when I use Edit or Delete it al goes wrong.

When cliking the Edit button I get an error in Details of the CustomerController because the CustomerEvent Id is passed in and not the Customer.Id

//
// GET: /Customer/Details/5
public ActionResult Details(int id)
{
    var customer = db.Customers.Single(c => c.ID == id);
   return View(customer);
}
When clicking the Delete button I get a similar error. The Delete action of the CustomerEventController has the Customer.Id in the id parameter not the actual CustomerEvent Id.

[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
    var customerEvent = db.CustomerEvents.Single(ce => ce.ID == id);
   int? customerId = customerEvent.CustomerId;
   db.DeleteObject(customerEvent);
    db.SaveChanges();
   return RedirectToAction("Details/" + customerId, "Customer");
}
         
I am obiviously doing something wrong here. I have a similar problem with another View where the View is strongly typed to a ViewModel object.

Please help me as these problems are showstoppers.

Best regards
PÃ¥l Eilertsen






7 Answers, 1 is accepted

Sort by
0
Fran
Top achievements
Rank 1
answered on 20 Jan 2011, 02:52 AM
I have the same problem with regards to the delete.

I posted it 2 days ago on this forum

http://www.telerik.com/community/forums/aspnet-mvc/grid/strange-hidden-input-generated-from-delete-command.aspx

The parent Id is getting passed in the delete form.

I haven't gotten a response, but I definitely think it's bug in the grid.
0
Pål
Top achievements
Rank 1
answered on 20 Jan 2011, 11:33 AM
Do you know the response times from Telerik on these forums? I have added several posts the last couple of days without any answers.

PÃ¥l
0
Atanas Korchev
Telerik team
answered on 20 Jan 2011, 12:52 PM
Hello,

We do not guarantee response by Telerik Staff in the ASP.NET MVC forums. This is highlighted in the support help page. If you need timely response consider purchasing a commercial license. It comes with 24 work hour response time. You can also refer to the licensing FAQ page on our web site.

The problem you are discussing is not a known issue. I suggest you check the server editing example for a working implementation. If this does not help consider attaching a runnable sample application.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Fran
Top achievements
Rank 1
answered on 20 Jan 2011, 03:31 PM
This example is different the the sample.  The sample mvc model is an IEnumerable<T>.  so the whole page is the grid.  In our examples, the Model is a a single type T with a child collection.  We are binding the grid to the model's child collection, not to the model.  and it sort of works right in that the the delete url created is correct.  the id on this url is of the child collection.  The problem is that the hidden delete id in the form is the parent id.  which is not correct.  they should match.

please see my forum posting on this.
0
Atanas Korchev
Telerik team
answered on 21 Jan 2011, 08:21 AM
Hello,

The hidden field is rendered based on the DataKeys that you have provided. Make sure the right data key is provided (the child one not the parent). Providing a sample project would speed up the problem resolution. 

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Fran
Top achievements
Rank 1
answered on 21 Jan 2011, 05:51 PM
I've created a test project that demonstrates this issue.

What I've discovered is that the hidden delete id is actually the id from the URL.

So in Asp.net mvc the default details page for an item is /controller/view/id

if you put a grid on the details page, the id from the url populates the hidden delete id.

Run the page included in the project I've attached with and without the id.

/Home/Index

produces the correct hidden input.

/Home/Index/a0cfc8f2-a79e-4685-b8a7-8f0c616b320b

does not.
0
Accepted
Atanas Korchev
Telerik team
answered on 24 Jan 2011, 10:28 AM
Hi,  

Thank you for providing a sample project. Here is why the observed behavior occurs:
  1. By default all DataKeys in the grid are named "id". 
  2. The grid uses HiddenFor() to render the data key of the item. Since the property is too named "Id" ASP.NET MVC uses the current route value for that property to set the value.
The workaround is to specify "RouteKey" for the grid dataKey. Then the correct value would be posted even if the route already contains a value for "ID". Unfortunately the hidden field will still point to the wrong value because the property of the Product is also named "ID".

I am sending the modified project.

 Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Pål
Top achievements
Rank 1
Answers by
Fran
Top achievements
Rank 1
Pål
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or