mvvm - Kendo UI Grid创建的数据未发送到 Controller

标签 mvvm kendo-grid

我很难使用this Kendo Dojo example中所示的MVVM方法将数据获取到 Controller
我可以在parameterMap函数中看到数据在options.models中,但是当我在 Controller 上查找数据时,FAC_FuelReceipts为空。我可以手动给我们一个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
    })
});

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

最佳答案

使用字符串而不是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);
        }

关于mvvm - Kendo UI Grid创建的数据未发送到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26692390/

相关文章:

c# - 使用 MVVM 更新依赖属性

asp.net-mvc - 当我在 Visual Studio 2013 中正在运行的另一个项目上打开一个项目时会发生什么?

javascript - JSON.stringify 奇怪的行为

javascript - 更改服务器在 kendo ui 网格中以编程方式排序

c# - 从 ViewModel 通知用户并取消导航

c# - 实现代码问题

c# - 使用MVVM时,XamlWriter可以读取文本框内容吗?

data-binding - 与用户控件的动态绑定(bind)不起作用,因为静态在 Silverlight 和 MVVM 中有效

javascript - KendoUI Grid 处理列标题的点击事件

angularjs - Kendo Grid 字段可编辑 :false not working with complex model