Changing the ContentType in the http request

17 posts, 0 answers
  1. Markus
    Markus avatar
    4 posts
    Member since:
    Apr 2013

    Posted 11 Apr 2013 Link to this post

    Hello,

    I am looking for a way to change the contenttype in the http request,  it seems you can do it in jquery but there doesn't seem to be a way to do it with the razor html helpers.  I would think it would work something like line #6.  Is this possible?

    Thanks,



    01..DataSource(dataSource => dataSource
    02.        .Ajax()
    03.        .PageSize(20)
    04.        .ServerOperation(true)
    05.        .Events(events => events.Error("error_handler"))y
    06.        .Create(update => update.Action("Customer_Create", "Customer", ContentType="Application/Json")
    07.                
    08.        )
    09.        .Read(read => read.Action("Customer_Read", "Customer"))
    10.        .Update(update => update.Action("Customer_Update", "Customer"))
    11.        .Destroy(update => update.Action("Customer_Delete", "Customer"))
    12.        .
    13.        
    14.    )
    15.)

       
  2. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 15 Apr 2013 Link to this post

    Hello Markus,

    This Kendo MVC Grid does not supported 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.

    Regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Markus
    Markus avatar
    4 posts
    Member since:
    Apr 2013

    Posted 15 Apr 2013 Link to this post

    Hi Daniel,  

    Are you suggesting I create the grid using javascript and not razor?  Maybe I am misunderstanding?  Or are you saying I can set the content type via javascript prior to rendering the grid with razor?  Can you provide an example of the latter if so?  The issue I am trying to resolve is a null reference error trying to execute the create action against our WCF odata service.  The object is populated all the way up to the point where it is trying to write the record back to the entity at which point it is null.  The create action works in fiddler but I can duplicate the error by omitting the content-type='Application/Json' parameter in the header.  I am thinking this may be the reason...



    -Markus
  5. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 17 Apr 2013 Link to this post

    Hello Markus,

    Yes, you should use the JavaScript initialization in order to be able to send the data as JSON. The MVC Grid DataSource uses form data. If you wish to use the wrappers for this scenario as well then it is possible possible to modify the options via JavaScript after the Grid 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);
        }
    });
    Kind regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Markus
    Markus avatar
    4 posts
    Member since:
    Apr 2013

    Posted 18 Apr 2013 Link to this post

    I am still having no luck with this, when the odataservice calls the business logic controller to write the record it passes a null reference....the customer object is populated up until the call to the business logic CreateEntity method. The Create Entity method is never passed the customer object and always throws a null reference.  I originally thought this was because of the content type but I am having my doubts now.  Accessing the WCF service directly with fiddler successfully creates the record...


    Here is the application level  Customer_Create action being called by the kendo grid,

    [System.Web.Http.HttpPost]
                    public ActionResult Customer_Create([DataSourceRequest] DataSourceRequest request, Application.ServiceProxy.CustomerDTO customer)
            {
     
                if (customer != null && ModelState.IsValid)
                {
                    var odataService = new Container(new Uri("http://localhost:8089/odata/"));
                    //odataService.r
                     odataService.AddToCustomer(customer);
                    odataService.SaveChanges();
     
                    return Json(ModelState.ToDataSourceResult());
     
                }
                return View("Index");
            }

    Here is the CreateEntity method being called through the generated WCF data service.

    protected override CustomerDTO CreateEntity(CustomerDTO customerDTO)
    {
         
        var customer = new customer()
        {
            matchcode = customerDTO.matchcode,
            salesOrganizationID = customerDTO.salesOrganizationID,
            statusID = customerDTO.statusID,
            serviceLevelStatusID = customerDTO.serviceLevelStatusID,
            mediaIDLogo = customerDTO.mediaIDLogo,
            systemID = customerDTO.systemID,
            customerExternalSystemID = customerDTO.customerExternalSystemID,
            internationalGroup = customerDTO.internationalGroup,
            division = customerDTO.division,
            corporateID = customerDTO.corporateID,
            inventoryManagerID = customerDTO.inventoryManagerID,
            dunsNumber = customerDTO.dunsNumber
        };
     
        entity.customer.Add(customer);
        entity.SaveChanges();
     
        return GetEntityByKey(customer.customerID);
    }




  7. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 22 Apr 2013 Link to this post

    Hello Markus,

    The code looks correct. A ViewResult should not be returned from the action even if there is a ModelState error but this should not cause problems with the binding. Could you provide a runnable project so I can investigate further?

    Regards,
    Daniel
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  8. Patrick
    Patrick avatar
    19 posts
    Member since:
    Jul 2010

    Posted 03 Nov 2013 Link to this post

    Hi,

    I've using this example and everything works great expect when the JSON is posted back to the server the sort prop is null. I'm using the DataSourceRequest binder on the action parameter "request". If I change it back to the default form post data (so I don't change the contentType option) all works with the sort prop. However I really need to post the data as JSON. Any help would be greatly appreciated!

    Patrick
  9. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 06 Nov 2013 Link to this post

    Hello,

    The DataSourceRequestAttribute does not support getting the values from the payload. If you wish to use JSON for the read request then you should implement a custom binder. If JSON should be used only for the create, update and destroy operations then you could check the type in the parameterMap function and use the "aspnetmvc-ajax" transport to serialize the values when read is performed:

    $(function () {
        var grid = $("#grid").data("kendoGrid"),
            mvcTransport = new kendo.data.transports["aspnetmvc-ajax"]({});
        grid.dataSource.transport.options.update.contentType = "application/json";
     
        grid.dataSource.transport.parameterMap = function (options, type) {
            var result;
            if (type == "read") {
                result = mvcTransport.parameterMap(options, type);
            }
            else {
                result = kendo.stringify(options);
            }
            return result;
        }
    });
    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  10. Robert
    Robert avatar
    16 posts
    Member since:
    Feb 2014

    Posted 08 Apr 2014 in reply to Daniel Link to this post

    If you wish to use JSON for the read request then you should implement a custom binder.


    I am trying to use JSON for the Read request and it is not working.  How do I implement a custom binder?

    Here is what I have:

        $(function ()
        {
            var grid = $('#JobsGrid').data("kendoGrid");
            grid.dataSource.transport.options.read.contentType = "application/json; charset=utf-8";
            grid.dataSource.transport.parameterMap = function (options, operation) { return JSON.stringify(postData); }
        });
  11. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 11 Apr 2014 Link to this post

    Hello Robert,

    Could you clarify why JSON should be used for read? The easiest option if this is required is to still serialize the state parameters to string values via the default parameterMap function. The alternative is to parse the collection of objects in the format specified in the documentation(sort, filter, group) to lists of state descriptors (SortDescriptor, GroupDescriptor and FIlterDescriptor). 
    On a side note, since the latest official release the contentType and parameterMap options as well as all other dataSource options can be set with the Custom builder. I attached a sample project which uses the Custom builder and the default parameterMap function to serialize the state values.

    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  12. Robert
    Robert avatar
    16 posts
    Member since:
    Feb 2014

    Posted 14 Apr 2014 in reply to Daniel Link to this post

    I need to use JSON because that is what MVC expects to receive in order to Deserialize the parameter.  

    So this is what I want to happen, I want to Post the JSON to the server and then my method would look like this:

            [HttpPost]
            public ActionResult GetJobs([DataSourceRequest]DataSourceRequest request, FilterViewModel filters)

    The filters should come in as JSON and then .Net can Deserialize them as the FilterViewModel object.

    The way it works now it is not coming in as JSON and therefore I have to declare my method like this:

            [HttpPost]
            public ActionResult GetJobs([DataSourceRequest]DataSourceRequest request, string filters)

    and then I have to do 
           FilterViewModel viewModel = new JavaScriptSerializer().Deserialize<FilterViewModel>(filters);
    in order to Deserialize the parameter.

  13. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 16 Apr 2014 Link to this post

    Hello again Robert,

    How are you sending the FilterViewModel with the read request? The MVC modelbinder also supports form data so the model should still be bound as expected with the default content type.

    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  14. Robert
    Robert avatar
    16 posts
    Member since:
    Feb 2014

    Posted 16 Apr 2014 in reply to Daniel Link to this post

    I am using JSON.stringify on the form data.
  15. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 17 Apr 2014 Link to this post

    Hello Robert,

    Isn't the data bound when not using JSON.stringify? In the latest versions, the additional data is also serialized as expected by the model binder for form data.

    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  16. Robert
    Robert avatar
    16 posts
    Member since:
    Feb 2014

    Posted 17 Apr 2014 in reply to Daniel Link to this post

    Here is my code.  I am open to how you would do it, but this is the only way I could get it work.

    The DataSource setting on my grid (MVC Razor):
            .DataSource(ds => ds
                .Ajax()
                .PageSize(50)
                .Read(read => read.Action("GetJobs", "Gremlin").Data("SendFilters").Type(HttpVerbs.Post))

    JavaScript SendFilters Method:
    function SendFilters()
    {
        var filters = new FilterViewModel();
        return { filters: JSON.stringify(filters) };
    };

    function FilterViewModel()
    {
        var self = this;
        self.JobName = $('#JobName').val();
        self.JobID = $('#JobID').val();
        self.Client = $("#Client").data("kendoDropDownList").value();
        self.Project = $("#Project").data("kendoDropDownList").value();
        self.Schematic = $("#Schematic").data("kendoDropDownList").value();
        self.JobNumber = $('#JobNumber').val();
        self.User = $('#User').val();
        self.Server = $('#Server').val();
        self.Source = $('#Source').val();
        self.RelativeStartTime = $('#RelativeStartTime').is(':checked');

        if (self.RelativeStartTime)
        {
            self.RelativeStartAmount = $('#RelativeStartAmount').val();
            self.RelativeStartInterval = $('#RelativeStartInterval').data("kendoDropDownList").value();
        }
        else
        {
            self.AbsoluteStartTime = $('#AbsoluteStartTime').data("kendoDateTimePicker").value();
        }

        self.RelativeEndTime = $('#RelativeEndTime').is(':checked');

        if (self.RelativeEndTime)
        {
            self.RelativeEndAmount = $('#RelativeEndAmount').val();
            self.RelativeEndInterval = $('#RelativeEndInterval').data("kendoDropDownList").value();
        }
        else
        {
            self.AbsoluteEndTime = $('#AbsoluteEndTime').data("kendoDateTimePicker").value();
        }

        self.Active = $('#Active').is(":checked");
        self.Completed = $('#Completed').is(":checked");
        self.Failed = $('#Failed').is(":checked");
        self.Paused = $('#Paused').is(":checked");
        self.Stopped = $('#Stopped').is(":checked");
        self.AWSJobs = $('#AWSJobs').is(":checked");
        self.ResubmittedOnly = $('#ResubmittedOnly').is(":checked");

        self.NA = $('#NA').is(":checked");
        self.Unchecked = $('#Unchecked').is(":checked");
        self.NeedsRevisited = $('#NeedsRevisited').is(":checked");
        self.NotAnIssue = $('#NotAnIssue').is(":checked");
        self.Fixed = $('#Fixed').is(":checked");
    };
  17. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 18 Apr 2014 Link to this post

    Hi,

    Based on the code that you provided, the model should be bound as expected with the default contentType and parameterMap function when the SendFilters function is changed as in the snippet below:
    function SendFilters() {
        var filters = new FilterViewModel();
        return filters;
    };


    Regards,
    Daniel
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  18. Robert
    Robert avatar
    16 posts
    Member since:
    Feb 2014

    Posted 21 Apr 2014 in reply to Daniel Link to this post

    That worked.

    Thank you.
Back to Top
UI for ASP.NET MVC is VS 2017 Ready