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

[Solved] Ajax Bound Grid insert not get parameter!

8 Answers 207 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.
Tony
Top achievements
Rank 1
Tony asked on 17 Feb 2011, 03:03 AM
I am using the latest MVC_2010_3_1318 extensions on Visual Studio 2010 and seem to be having trouble using Ajax calls to update the database via the Grid.
In View return parameter "orderid", but control can not get this parameter value! it is value always equal zero.
View:

<%= Html.Telerik().Grid<Demo_MasterDetail.Models.Order_Details>()
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.ProductID))
    .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text).ImageHtmlAttributes(new { style = "margin-left:0" }))
    .DataBinding(dataBinding => 
    {
        dataBinding.Ajax()
            .Select("_SelectAjaxEditing", "Home", new { orderid = Model.orders.OrderID })
            .Insert("_InsertAjaxEditing", "Home", new { orderid = 99999 })  // <--------------- parameter
            .Update("_UpdatejaxEditing", "Home", new { orderid = Model.orders.OrderID })
            .Delete("_DeleteAjaxEditing", "Home", new { orderid = Model.orders.OrderID }); 
    })        
    .Columns(columns =>
    {
        columns.Bound(o => o.OrderID).Width(80);
        columns.Bound(o => o.ProductID).Width(80);
        columns.Bound(o => o.UnitPrice).Width(80).Format("{0:N}").HtmlAttributes(new { style = "text-align: right" });
        columns.Bound(o => o.Quantity).Width(80).Format("{0:N}").HtmlAttributes(new { style = "text-align: right" });
        columns.Bound(o => o.Discount).Width(80);
        columns.Command(commands =>
        {
            commands.Edit().ButtonType(GridButtonType.Text);
            commands.Delete().ButtonType(GridButtonType.Text);
        }).Width(180).Title("Commands");
    })
    .Pageable()
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .Sortable()
    .Scrollable(scrolling => scrolling.Height(350))
%>

controller:
[HttpPost]
[GridAction]
public ActionResult _InsertAjaxEditing(int orderid)  // <-----  orderid always = 0
{
    Order_Details tmporderdetails = new Order_Details();
    TryUpdateModel(tmporderdetails);
    _myDataRepository.Insert(tmporderdetails);
    return View(new GridModel(_myDataRepository.All(orderid)));
}

8 Answers, 1 is accepted

Sort by
0
Tony
Top achievements
Rank 1
answered on 18 Feb 2011, 01:51 AM
Any body please help me?
0
Mark
Top achievements
Rank 1
answered on 07 Apr 2011, 11:28 PM
Hi Tony,
Did you find a solution to this problem? I am trying to do the same, but failing like you.

Let me know if you found a solution or workaround.

Mark.
0
Alexandre
Top achievements
Rank 1
answered on 16 May 2011, 10:01 AM
Hi, 
Same issue for me ! 
Please, help us !
0
Vasile
Top achievements
Rank 1
answered on 13 Aug 2011, 08:13 PM
I have spent horible amount of time to find a solution for this issue. There are lot of threads asking for a solution but no answers...
I have found a workaround that I will use until Telerik is going to finally provide an answer or a solution. This would work with GridEditMode.PopUp - didn't explored the other options.

My scenario is as following: I use TreeView to select a master entity then in a Grid I insert detail records with Ajax.

I have created an EditorTemplate for my slave entity. In that template I added a hidden field for the MasterId (which is also property of the slave entity)

On TreeView I handle onSelected client-side event (if you use Grid for the master records there exists a similar onRowSelect client event), on this even I store into a globa variable the id of the master record.

I handle the client event onSave of the details grid, and there I have the opportunity to set manually the MasterId using

 

e.values["MasterId"] = gMasterId;

My Insert action in controller will not receive any parameter, the id will be populated directly by TryUpdateModel from the hidden's field value.

I feel that this is not how it shall work, but I was not able to find another way. Hopfully Telerik will provide soon a solution for this annoying issue.

A more elegant alternative would have been if in the onSave() Grid client event I it would have been possible to extend the e.data with the masterId parameter, like I do in onDataBinding event
function onDataBinding(e) { 
    e.data = $.extend(e.data, { masteId: gMasterId });
}

Now if you extend e.data in the onSave event .. it is just ignored. If this would work then no need for EditTemplate & hidden Fields and would have work with all GridEditModes, all most nice.
0
Atanas Korchev
Telerik team
answered on 15 Aug 2011, 09:27 AM
Hello Vasile,

 The recommended way to send additional values to the server is via the values event argument of the OnSave event. I think it is actually the same approach as e.data when using OnDataBinding - just the arguments name is different.


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
Vasile
Top achievements
Rank 1
answered on 16 Aug 2011, 06:16 PM
Hello Atanas Korchev,

I have checked now and it works with passing it via "values" event parameter. I had two misleading assumptions:
1. I was assuming that e.data is used for action's parametes and e.values for the fields that gets posted to the server and populated by TryUpdateModel.
2. I found only samples where in the onSave event,  were only manipulation of existing fields (e.g. provide default values) that's why I tought I will need a hidden field for it.

using OnSave and e.Values["MasterId"] = id... will propagate the parameter into TryUpdateMode, but also into the action's parameter if a proper route is available.

Thanks for clarifying this for us.
Vasile
0
Thomas
Top achievements
Rank 1
answered on 07 Aug 2012, 10:09 PM

 

I have seen/had this problem before & it drove me NUTS!
I think the problem is due to a binding conflict.
Place a break on the first line of the _InsertAjaxEditing Action.
If you look at the Request QueryString (in the immediate window: ?Request.QueryString) the parameter should be there correctly: orderid=9999.
But there is also an OrderID on the form. In the immediate window: ?HttpContext.Request.Form["OrderID"] the value is zero.

I think that when the Action's parameters are bound, MVC is using the OrderID in the form (ignoring the upper case letters).  Apparently, the binder is not ‘case’ sensitive.

I have always fixed this by changing the action parameter name to one not used in the form or model.
Try setting the ajax route value as: .Insert("_InsertAjaxEditing", "Home", new { idOrder = 99999 })
and define the action: public ActionResult _InsertAjaxEditing(int idOrder)

This has worked for me.

0
M
Top achievements
Rank 1
answered on 03 Jun 2013, 04:58 PM
Hi, I have the same problem. Could you please tell me exactly how to deal with this. Thanks!
Tags
Grid
Asked by
Tony
Top achievements
Rank 1
Answers by
Tony
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Alexandre
Top achievements
Rank 1
Vasile
Top achievements
Rank 1
Atanas Korchev
Telerik team
Thomas
Top achievements
Rank 1
M
Top achievements
Rank 1
Share this question
or