将Kendo Grid数据发布到MVC中的Controller

tvm*_*jie 6 javascript asp.net-mvc grid kendo-ui

我有两节课.一个包含其他类的列表:

public string Name { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public List<Models.Occupation> Occupations { get; set; }
Run Code Online (Sandbox Code Playgroud)

第二类如下

public string Name { get; set; }
public string Industry { get; set; }
Run Code Online (Sandbox Code Playgroud)

我的控制器呈现视图

Person p = new Person()
{
     Name = "megan",
     Surname = "du Preez",
     Id = 0,
     Age = 22
 };
 return View(p);
Run Code Online (Sandbox Code Playgroud)

在视图中

@model Models.Person

<form id="person">
    <div>
        @Html.TextBoxFor(mp => mp.Name)
        @Html.TextBoxFor(mp => mp.Surname)
        @(Html.Kendo().Grid<Models.Occupation>()
        .Name("Occupations")
        .Columns(columns =>
            {
                columns.Bound(e => e.Name);
                columns.Bound(e => e.Industry);
            })
        )
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("Occupations_Read", "People", new { Name = Model.Name}))
        )
    </div>

    @Html.Kendo().Button().Name("btnTest").Content("Create")
Run Code Online (Sandbox Code Playgroud)

这读取人员职业如下

List<Models.Occupation> oc = new List<Models.Occupation>();
oc.Add(new Models.Occupation() { CategoryName = "Lecturer" });
oc.Add(new Models.Occupation() { CategoryName = "Student" });
oc.Add(new Models.Occupation() { CategoryName = "Janitor" });

return Json(oc.ToDataSourceResult(request));
Run Code Online (Sandbox Code Playgroud)

所有这些都呈现了我的观点,一切正常,但在表单帖子上我想将所有内容发送回动作

[HttpPost]
public JsonResult PersonPost(Models.Person p)
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

我可以使用以下javascript轻松发布Person的名字和Surname

$("#btnTest").click(function (e) {
    e.preventDefault();

    $.ajax({
        url: "/Tasks/PersonPost",
        type: 'POST',
        data: $('#person').serialize(),
        dataType: 'json',
        success: function (data) {

        }
    });
});
Run Code Online (Sandbox Code Playgroud)

但网格中的职业不会被序列化并回发到控制器操作.

我的问题是如何将整个模型与视图中的职业列表发布到控制器.

Sha*_*haz 10

试试这个..

    @(Html.Kendo().Grid<Models.Occupation>()
    .Name("Occupations")
    .Columns(columns =>
    {
        columns.Bound(e => e.Name).ClientTemplate("#= Name # <input type='hidden' name='Occupation[#=index(data)#].Name' value='#= Name #' />");
        columns.Bound(e => e.Industry).ClientTemplate("#= Industry # <input type='hidden' name='Occupation[#= index(data)#].Industry' value='#= Industry#' />");
    })        
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Occupations_Read", "People", new { Name = Model.Name}))
    )

[HttpPost]
public JsonResult PersonPost(Models.Person p)
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

你应该能够在Person中获得价值.请添加以下功能.. **************Javascript **************

 //Index function for the WorkOrder Create view
        function index(dataItem) {

            var data = $("#GridName").data("kendoGrid").dataSource.data();
            return data.indexOf(dataItem);
        }
Run Code Online (Sandbox Code Playgroud)

Shaz