Kendo UI Grid创建数据而不是控制器

时间:2014-11-01 19:38:04

标签: mvvm kendo-grid

我很难使用MVVM方法将数据传输到我的控制器,如this Kendo Dojo example所示 我可以在我的parameterMap函数中看到数据在options.models中,但是当我在控制器上查找数据时,FAC_FuelReceipts为null。我可以手动给我们一个ajax调用,但我想让它首先工作“开箱即用”。我做错了什么?

网格:

$("#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代码:

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
    })
});

控制器代码:

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

1 个答案:

答案 0 :(得分:1)

使用String而不是IEnumerable,因为您的参数数据是字符串格式。 一旦获得字符串格式的数据反序列化到您的对象

[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);
        }