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

Binding Create action, null model

3 Answers 139 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 18 Sep 2012, 09:23 AM
Hello I am tryint to bind create action from Kendo Grid:

@(Html.Kendo().Grid(Model.GlobalProperties).Name("GlobalProperties")
    .Columns(columns =>
    {
        columns.Bound(p => p.Id).Hidden();
        columns.Bound(p => p.Name);
        columns.Bound(p => p.Value);
    })
    .DataSource(dataSource => dataSource.Ajax().Batch(true)
        .Model(model => model.Id(p => p.Id))
        .Create("GlobalProperty_Create", "Admin")
        .Update("GlobalProperty_Editing_Update", "Admin")
        .Destroy("GlobalProperty_Editing_Destroy", "Admin")
    )
    .ToolBar(toolbar =>
    {
        toolbar.Create();
        toolbar.Save();
    })
    .Editable(editable => editable.Mode(GridEditMode.InCell))
)

This is supposed to send Create action to my Admin controller (this works)

public ActionResult GlobalProperty_Create(List<GlobalProperties> models)
       {
           using (var db = new CPSkla.Models.CPSklaEntitiesCalculation())
           {
               foreach (GlobalProperties model in models)
               {
                   db.GlobalProperties.Add(model);
                   db.SaveChanges();
               }
           }
           return View(GetViewResult());
       }

Now if I try to create ne record it will trigger my breakpoint in controller but the list (although it has Count=1 for one new record) has GlobalProperty object but with all values (Id,Name,Value) null.

My firebug shows that I have
models[0][id]  0
models[0][name] test
models[0][value] testssss
as a part of my parametres, how do I serialize that to my List?

Please help I was trying to find it but without success.

3 Answers, 1 is accepted

Sort by
0
Nohinn
Top achievements
Rank 1
answered on 18 Sep 2012, 10:13 AM
I don't know about server wrappers (you should ask in the grid forum of mvc server wrappers).
In the javascript way you should use the parameterMap function of the dataSource, and  to achieve that your controller gets the items, the key of each property should be "models[0].Property".
So, in your case:
Key - Value
models[0].id - 0
models[0].name - test
models[0].value - testssss 
0
David
Top achievements
Rank 1
answered on 18 Sep 2012, 11:18 AM
Nohinn: Thx for your reply. I know this approach, but I was trying to use more clean approach. I know I can use

string name = Request.Params["models[0][Name]"];

But this seems a little bit too unortodox for me. Its just about that serialization.

Sorry if I am in wrong thread, can admin please move this to the right Grid wrapper section?
0
Nohinn
Top achievements
Rank 1
answered on 18 Sep 2012, 11:29 AM
Sorry if I didn't explain myself.
I'm working with mvc (but without wrappers as I don't have a license), what I was trying to say is that in javascript you should transform the data that the control will send to the controller to match the structure I posted.
parameterMap: function(options, action) {
    var newOptions = [];
    for(var i = 0; i < options.models.length; i++) {
        for(var prop in options.models[i]) {
            newOptions["models[" + i + "]." + prop] = models[i][prop];
        }
    }
    return newOptions;
}
This resulting structure would bind directly to your action parameter, so you would only have to iterate through the list of models and get the property of each item.
string name = models[0].Name;
 As you see, this would be done through javascript, I don't know if there is any way to do it directly with the wrappers.
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Nohinn
Top achievements
Rank 1
David
Top achievements
Rank 1
Share this question
or