Alan Fisher
Alan Fisher

Reputation: 2055

Kendo UI Grid create data not making it to controller

I am having difficulty getting data to my controller using the MVVM method as shown in this Kendo Dojo example I can see in my parameterMap function that the data is in the options.models but when I look for data at the controller, FAC_FuelReceipts is null. I can manually us an ajax call but I want this to work "Out of the Box" first. What am I doing wrong?

Grid:

$("#grid").kendoGrid({
    height: 430,
    columns: [
        { field: "FuelReceiptID" },
        { field: "ReceiptDate", title: "Receipt Date", width: 110, format: "{0:MM/dd/yyyy}" },
        { field: "FuelType", title: "Fuel Type", width: 110, editor: fuelTypeDropDownEditor },
        { field: "Qty", width: 110 },
        { field: "ReceivedBy", width: 110 }

    ],
    editable: true,
    pageable: true,
    sortable: true,
    filterable: true,
    navigatable: true,
    toolbar: ["create", "save", "cancel"],
    dataSource: viewModel.receipts
});

ViewModel Code:

var viewModel;
$(function () {  //On Ready
viewModel = kendo.observable({
    receipts: new kendo.data.DataSource({

        schema: {
            model: {
                id: "FuelReceiptID",
                fields: {
                    FuelReceiptID: { editable: false, nullable: true },
                    ReceiptDate: {  type: "date",    validation: { required: true } },
                    FuelType: { type: "string", defaultValue:"Diesel" },
                    Qty: { type: "number", validation: { required: true } },
                    ReceivedBy: { type: "string" }
               }
            }
        },
        batch:true,
        transport: {
            read: {
                cache:false,
                url: "/Fuels/GetFuelReceipts",
                dataType: "json"

            },
            create: {
                url: "/Fuels/Create",
                dataType: "json",
                type: "POST"
            },

            parameterMap:function(options,operation){

                if (operation == "read") {
                    return{

                        SiteID: SiteID,
                        ReceiptMonth: ReceiptMonth,
                        ReceiptYear: ReceiptYear
                    }
                }

                if (operation !== "read" && options.models) {
                    return { FAC_FuelReceipts: kendo.stringify(options.models) };
                   }
            }  //parameterMap fuction
        } //transport
    })
});

Controller Code:

 [HttpPost]
    public JsonResult Create(IEnumerable<FAC_FuelReceipts> FAC_FuelReceipts) //**empty here**
    {
       //Do something with data here
       return Json(FAC_FuelReceipts, JsonRequestBehavior.AllowGet);
    }

Upvotes: 1

Views: 199

Answers (1)

Abbas Galiyakotwala
Abbas Galiyakotwala

Reputation: 3019

Use String instead of IEnumerable, As your parameter data is in string format. Once you get data in string format deserialize into your object

[HttpPost]
public JsonResult Create(string FAC_FuelReceipts)
        {

            IList<FAC_FuelReceipts> Items= new JavaScriptSerializer().Deserialize<IList<FAC_FuelReceipts>>(FAC_FuelReceipts);

            /**your code*/

           return Json(FAC_FuelReceipts);
        }

Upvotes: 1

Related Questions