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.
)
16 Answers, 1 is accepted
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
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
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);
}
});
Daniel
the Telerik team
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);
}
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
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
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;
}
});
Daniel
Telerik
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); }
});
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.
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.
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.
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.
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");
};
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.
Thank you.