I am trying to get a Form containing a telekik MVC grid and other elements to submit.
- The View model contains contains three string fields and an
IEnumerable
collection. - The grid is server bound. I am not adding any elements or deleting any elements from the list using the grid but the grid contains a check box mapped to a boolean column in the list items.
When ever i submit this form the three string elements return in the post method but list is always null.
Here is the data model:
public class Parent
{
public string Field1{get;set;}
public string Field2{get;set;}
public string Comments {get;set;}
public IEnumerable<ChildItems> Children{get;set;}
}
Public class ChildItems
{
public string ChildField1{get;set;}
public string ChildField2{get;set;}
public boolean Include{get;set;}
}
Here is my View:
@{
ViewBag.Title = "Index";
}
@model GridInForm.Models.Parent
@using(Html.BeginForm("Save", "Home"))
{
<fieldset>
<legend>Editing Parent</legend>
@Html.LabelFor(parent => parent.Field1)
@Html.EditorFor(parent => parent.Field1)
@Html.LabelFor(parent => parent.Field2)
@Html.EditorFor(parent => parent.Field2)
@Html.LabelFor(parent => parent.Comments)
@Html.EditorFor(parent => parent.Comments)
@(Html.Kendo().Grid(Model.Children)
.Name("Children")
.ToolBar(tools => tools.Create().Text("Add new Children"))
.Editable(editable => editable.Mode(GridEditMode.PopUp)
.CreateAt(GridInsertRowPosition.Bottom))
.Columns(columns =>
{
columns.Bound(p => p.ChildField1).ClientTemplate("#= ChildField1 #" +
"<input type='hidden' name='ChildField1[#= index(data)#].ChildField1'
value='#= Name #' />"
);
columns.Bound(p => p.ChildField2).Hidden().ClientTemplate("#= ChildField1
#" +
"<input type='hidden' name='ChildField1[#= index(data)#].ChildField1'
value='#= ChildField1 #' />"
);
columns.Command(command =>
{
// command.Destroy();
command.Edit();
}
).Width(100);
})
.DataSource(dataSource => dataSource.Server().Create("Create",
"Home").Read("Index", "Home").Update("Update", "Home")
.Model(model =>
{
model.Id(p => p.ChildField1);
model.Field(p => p.ChildField1).Editable(false);
})
//.ServerOperation(true)
)
)
</fieldset>
<input type="submit" value="Save" />
}
<script>
function index(dataItem) {
alert("bindind");
var data = $("#Products").data("kendoGrid").dataSource.data();
return data.indexOf(dataItem);
}
</script>
WHen i submit the form I get the parents items back in the view model but the Inumerable fields from the grid are always null..
Is this just not the way to do this and if so what is the way to acommplish something like this. I have had this issue on previous telerik versions and I see it is the same on Kendo UI. Any direction would be greatly appreciated. This has been along going on issue.
I was inspired by codebeastie and @user1878526 to produce the following hybrid idea - simply returning the grid as a list of strings to the controller save action:
Controller:
View:
This can then be parsed in the controller reasonably easily by 'Split'ing on commas and colons.
So here is part of a solution to your question. I extended the kendo Datasource, so you can call this function and get all the updated, deleted, created items out. Just turn it into json and send it back with the rest of your form fields. I have got this working now. Thought I would share.
I have exactly this scenario working perfectly in my project. Here's my grid declaration...
Here's the "index" function: