简体   繁体   English

如何使用JSON作为contentType来制作Kendo MVC Helpers的CRUD

[英]How to make Kendo MVC Helpers's CRUD using JSON as contentType

 @(Html.Kendo().DropDownListFor(model => model.ServiceID) .OptionLabelTemplate("#=optionLabel#") .ValueTemplate("#=Code#(#=Rate#) - #=Description#") .Template("#=Code#(#=Rate#) - #=Description#") .DataTextField("Code") .DataValueField("ServiceID") .DataSource(d => { d.Read(read => { read.Action("GetServiceRepository", "Service").Data("...") .Type(HttpVerbs.Post); }); }) .OptionLabel(new { optionLabel = Resources.Wording.SelectOne, ServiceID = 0, Rate = 0, Code = "" }) ) 

I have a Kendo Dropdownlist which initialized using HTML helper way instead of JQuery way. 我有一个Kendo Dropdownlist,它使用HTML帮助方式而不是JQuery方式初始化。

Is there anyway to make the post request to /Service/GetServiceRepository using JSON as contentType instead of the default application/x-www-form-urlencoded ? 无论如何使用JSON作为contentType而不是默认的application/x-www-form-urlencoded来向/ Service / GetServiceRepository发布帖子请求?

You can set the ContentType property using DataSource's Custom fluent method. 您可以使用DataSource的Custom fluent方法设置ContentType属性。 I use version 2016.2.504. 我使用版本2016.2.504。

The usage is: 用法是:

 @(Html.Kendo().DropDownListFor(model => model.ServiceID)
  .DataTextField("Text")
  .DataValueField("Value")
  .DataSource(d => d.Custom()
    .Transport(c => c.Read(
      read => read.ContentType("xml/json")
          .Data("...")
          .Type(HttpVerbs.Post)
          .Action("GetServiceRepository", "Service")))
  ))

This Kendo MVC Helper does not support setting the content type. 此Kendo MVC Helper不支持设置内容类型。 It is designed to work with the MVC controllers and the Kendo MVC server API and so not all request options can be set. 它旨在与MVC控制器和Kendo MVC服务器API一起使用,因此不能设置所有请求选项。 You should use the JavaScript initialization in order to be able to set all options. 您应该使用JavaScript初始化以便能够设置所有选项。 It is possible to modify the options via JavaScript after the helper has already been initialized eg 在帮助器初始化之后,可以通过JavaScript修改选项,例如

$(function () {
    var grid = $("#grid").data("kendoGrid");
    grid.dataSource.transport.options.update.contentType = "application/json";
    //override the parameterMap function in order to convert the data to JSON
    grid.dataSource.transport.parameterMap = function (options, type) {
        return kendo.stringify(options);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM