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

[Solved] Grid double posting on insert

2 Answers 23 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.
Morgan
Top achievements
Rank 1
Morgan asked on 16 Mar 2012, 04:47 PM
I have a simple grid with two colums, Name and Value.  The items are stored in a simple object with two properties as follows: 

 

[Serializable]
public class NameValuePairModel
{
    private string name;
    private string value;
 
    public string Name
    {
        get {return name;}
        set {name = value;}
    }
 
    public string Value
    {
        get {return value;}
        set {this.value = value;}
    }
}

The objects are stored in a Dictionary<string, NameValuePairModel> object, which is persisted to session state.

When I insert the first item it works fine.  Inserting the second item results in an exception from trying to insert a duplicate record into the dictionary.  Tracing through the code, I find that the InsertParameter method is being called once for each row in the grid instead of just for the row that's being added. 

The controller code is as follows:
[GridAction]
public ActionResult InsertParameter()
{
    Dictionary<string, NameValuePairModel> parameters = GetParametersFromSession();
 
    NameValuePairModel model = new NameValuePairModel();
 
    if (TryUpdateModel(model))
    {
        parameters.Add(model.Name, model);
        Session["testParameters"] = parameters;
    }
 
    return View(new GridModel<NameValuePairModel>
                {
                    Data = GetParameterGridData(parameters)
                });
}

And the grid code in the view is as follows:

<div class="sizedContainer" style="height: 240px; background-color: #696969">
    @(Html.Telerik().Grid<NameValuePairModel>()
            .Name("Rulesets")
            .DataKeys(keys => keys.Add(p => p.Name))
            .ToolBar(commands => commands.Insert().ButtonType(GridButtonType.Text))
            .Columns(columns =>
                     {
                        columns.Bound(o => o.Name).Title("Name").Filterable(false).Sortable(true);
                        columns.Bound(o => o.Value).Title("Value").Filterable(false).Sortable(true);
                        columns.Command(commands =>
                                        {
                                            commands.Edit().ButtonType(GridButtonType.Image);
                                            commands.Delete().ButtonType(GridButtonType.Image);
                                                     });
                            })
            .DataBinding(
                    dataBinding => dataBinding.Ajax().OperationMode(GridOperationMode.Server)
                                    .Select("GetParameters", "TestHarness")
                                    .Insert("InsertParameter", "TestHarness")
                                    .Delete("DeleteParameter", "TestHarness")
                                    .Update("UpdateParameter", "TestHarness"))
            .Sortable()
            .Editable(editing => editing.Mode(GridEditMode.InLine))
            .Pageable(
                    paging => paging.PageSize(5)
                                .Style(GridPagerStyles.NextPreviousAndNumeric | GridPagerStyles.PageSizeDropDown)
                                .Position(GridPagerPosition.Both)
            ))
    

I can't figure out why it's doing this.  The demo doesn't seem to do this, but for some reason every time I try to use the data bound editing with the grid my code does it.

Any assistance would be appreciated.

I can code around it by checking to see if the collection already contains the key, but that's a bit kludgy and I'd prefer not to.

Thanks!

Morgan

2 Answers, 1 is accepted

Sort by
0
Morgan
Top achievements
Rank 1
answered on 21 Mar 2012, 04:57 AM
Bump to top of posts.
0
Daniel
Telerik team
answered on 21 Mar 2012, 02:16 PM
Hello Morgan,

This is unknown issue and I couldn't reproduce it based on the code you have provided. Could you send a sample project which replicates this behavior so I can check the exact setup?

Kind regards,
Daniel
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Grid
Asked by
Morgan
Top achievements
Rank 1
Answers by
Morgan
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or