Post Kendo Grid data to Controller in MVC

痞子三分冷 提交于 2019-12-05 07:47:06
Shaz

Try this..

    @(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
}

you should be able to get values in Person. please add the following function.. *****************************Javascript***************************

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

            var data = $("#GridName").data("kendoGrid").dataSource.data();
            return data.indexOf(dataItem);
        }

Shaz

You can use this script on your event:

function SaveUserProjectDetails() {

        var postUrl;
        var paramValue;

        var gridData = $("#CustomerInfoKendoGrid").data("kendoGrid").dataSource.data();
        postUrl = '@Url.Content("~/Billing/CustomerAccountManage/GetDepartmentWiseCategoryList")';
        paramValue = JSON.stringify({ CustomerInformationList: gridData });


        $.ajax({
            url: postUrl,
            type: 'POST',
            dataType: 'json',
            data: paramValue,
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
                console.log(result);
            },
            error: function (objAjaxRequest, strError) {
                var respText = objAjaxRequest.responseText;
                console.log(respText);
            }
        });

    }

CustomerInformationList is your grid source list. For more details see this

The grid data isn't in form elements. The form elements appear only when a cell is being edited, then it is removed. You can't post the data to the server by using a form submit button.

The proper way to to this would be by adding the 'save' command button that the grid provides itself:

@(Html.Kendo().Grid<Invoice.Models.ViewModels.SegmentViewModel>()
    .Name("Segment")
    .ToolBar(toolbar => {
        toolbar.Save(); // add save button to grid toolbar
    })
    // ... rest of options ...

Or by calling saveChanges() on the Grid widget:

Save Segments

$("#save").on("click", function () {
    $("#Segment").data("kendoGrid").saveChanges();
});

What about an solution like this:

$( document ).ready(
   $("form").each(function(i, form){
       $(this).find(".k-grid").each(function(_i, div){
           $(form).submit(div, kendoGridSubmitData);
       });
    });
);

function kendoGridSubmitData(e) {
    var lModel = e.data.id;
    var lKendoGrid = $(e.data).data("kendoGrid");

    // Iterate over all rows
    lKendoGrid.dataItems().forEach(function(_row, _rowIndex){
        // Iterate over all fields
        _row.forEach(function(_value, _name){
            // Add the input hidden
            $('<input>').attr({
                type: 'hidden',
                id: lModel,
                name: lModel+'['+_rowIndex+'].'+_name,
                value: _value
            }).appendTo('form');
        });
    });
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!