8

@(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.

Is there anyway to make the post request to /Service/GetServiceRepository using JSON as contentType instead of the default application/x-www-form-urlencoded?

shole
  • 4,046
  • 2
  • 29
  • 69

2 Answers2

3

You can set the ContentType property using DataSource's Custom fluent method. I use version 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")))
  ))
fduman
  • 190
  • 4
  • You didn't elaborate which Kendo version you were using. I think this is the answer that you are looking for. Eventually you will use 2016 version because of bugfixes. – fduman May 11 '16 at 06:56
3

This Kendo MVC Helper does not support setting the content type. It is designed to work with the MVC controllers and the Kendo MVC server API and so not all request options can be set. You should use the JavaScript initialization in order to be able to set all options. It is possible to modify the options via JavaScript after the helper has already been initialized e.g.

$(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);
    }
});
rohitreddyk
  • 337
  • 2
  • 15