Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 269
shole Avatar asked Apr 29 '16 08:04

shole


2 Answers

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);
    }
});
like image 103
rohitreddyk Avatar answered Oct 20 '22 07:10

rohitreddyk


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")))
  ))
like image 3
fduman Avatar answered Oct 20 '22 08:10

fduman