如何使用mvvm kendo网格发布数据并发布api控制器而不是调用?

时间:2015-05-25 08:56:10

标签: c# asp.net mvvm kendo-grid kendo-mvvm

我想将数据从kendo网格发送到sql数据库,这是我的javascript视图模型代码:

  document.onreadystatechange = function () {
var viewModel = kendo.observable({

    products: new kendo.data.DataSource({

    schema: {
       //data:"Data",
        total: "Count",

        model: {
            Id: "Id",
            fields: {
                Id: { editable: true, type: "int" },
                ShortName: { editable:true, type: "string" },
                FullName: { editable: true, type: "string" },
                ContactPerson: { editable: true, type: "string" },
                CurrentCurrencyCode: { editable: true, type: "int" },
                Adress1: { editable: true, type: "string" },
                CompanyState: { editable: true, type: "string" },

                CompanyCity: { editable: true, type: "string" },
                CompanyCountry: { editable: true, type: "string" },
                ZipPostCode: { editable: true, type: "string" },
                TelArea: { editable: true, type: "string" }

            }
        }
    },
    batch: true,

    transport: {
        read: {
            url: "/api/Companies/GetAllCompanies",
            dataType: "json"
        },
        create:{

            url: "/api/Companies/SaveDefCompny", // here is a correct api url, which i want to call
            dataType: "json"
        },

        destroy: {
            url: "/api/Companies/Delete", 
            dataType: "json"
        },
        parameterMap: function (data, operation) {
            if (operation !== "read" && data) {
                return  kendo.stringify(data) ;
            }
        }
    }

})
});
kendo.bind(document.getElementById("example"), viewModel);


}

这是我的控制器代码,用于将数据发布到数据库但是没有通过单击创建或更新按钮调用我的网格或控制器调用的问题?

    [HttpPost]
    public void SaveDefCompny(IEnumerable<DefCompanyDTO> DfCmpny1)
    {

        var result = new List<DefCompany>();
        using (var data = new RPDBEntities())
        {
            foreach (var productViewModel in DfCmpny1)
              {
                var product = new DefCompany
                {
                  Id = productViewModel.Id,
                    CurrentCurrencyCode = productViewModel.CurrentCurrencyCode,
                    ShortName= productViewModel.ShortName,
                    FullName= productViewModel.FullName,
                    ContactPerson= productViewModel.ContactPerson,
                    Address1= productViewModel.Address1,
                    CompanyCity= productViewModel.CompanyCity,
                    CompanyState= productViewModel.CompanyState,
                   CompanyCountry= productViewModel.CompanyCountry,
                   ZipPostCode= productViewModel.ZipPostCode,
                   TelArea= productViewModel.TelArea

                };
               result.Add(product);
               data.DefCompanies.Add(product);
          };
             data.SaveChanges();
        }

    }

url是正确的,但即使调试游标没有进入url但是网格读取所有值并显示在其中,它也不会调用

1 个答案:

答案 0 :(得分:1)

当您要发布数据时,创建方法中缺少type: "POST"

并检查发布data.models而不是data