I am not sure what I am missing but I am unable to send an object as a parameter to a controller, the request keep failing with 500 (Internal Server Error).
C# Object:
public class RateQueryDto{ public long WorkHistoryId { get; } public int WorkerId { get; } public int ClientId { get; } public DateTime WeekEndingDate { get; }}
MVC Controller method:
[HttpPost]public ActionResult Read([DataSourceRequest] DataSourceRequest request, RateQueryDto rateQuery){ return Json(true);}
Grid configuration:
@(Html.Kendo().Grid<ConfirmHoursRateModel>() .Name("grid") .HtmlAttributes(new { style = "height:250px;" }) .AutoBind(false) .Columns(columns => { columns.Bound(c => c.Id).Hidden(true); }) .Editable(editable => editable.Mode(GridEditMode.InCell)) .Navigatable() .DataSource(dataSource => dataSource .Ajax() .ServerOperation(false) .Batch(true) .Model(model => { model.Id(w => w.Id); }) .Read(read => read.Action("Read", "ConfirmHours").Data("confirmHoursReadData"))) )
Javascript:
function confirmHoursReadData() { var grid = $("#confirm-hours-grid").data("kendoGrid"); var selectedItem = grid.dataItem(grid.select()); return { WorkHistoryId: selectedItem.WorkHistoryID, WorkerId: selectedItem.WorkerID, ClientId: selectedItem.ClientID, WeekEndingDate: selectedItem.WeekEndingDate };}
11 Answers, 1 is accepted
When passing additional values using the Data method the names of the fields should match the arguments in the respective Action Method. The Read action would look similar to the following:
public ActionResult Read([DataSourceRequest] DataSourceRequest request, int WorkHistoryId, int WorkerId, int ClientId){ return Json(true);}Give the modification a try and let me know how it works for you.
Regards,
Viktor Tachev
Progress Telerik
Hi Viktor,
That always work and currently I am using it as a workaround, however I would like to transform more than three parameter into a DTO. I've tried below but no luck:
function confirmHoursReadData () { var grid = $(confirmHoursGrid).data("kendoGrid"); var selectedItem = grid.dataItem(grid.select()); var _rateQueryDto = { workHistoryId: selectedItem.WorkHistoryID, workerId: selectedItem.WorkerID, clientId: selectedItem.ClientID, weekEndingDate: selectedItem.WeekEndingDate }; return { rateQueryDto: _rateQueryDto };};The result from the function that is passed to the Data() method should be JSON. Thus, you can use custom JavaScript to collect the data and stringify it.
function additionalData(e) { return { complexItem: JSON.stringify({ field1: "sample value", field2: "some other value" }) };}Then you can parse it on the server using DeserializeObject method.
var jsonData = JsonConvert.DeserializeObject(complexItem);Regards,
Viktor Tachev
Progress Telerik
Hi Viktor,
I've tried above approach but it only works if I change my controller method's signature to accept a dynamic object?
If the names of the fields in the object passed from the client correspond to the fields specified in the actual Model the information should be parsed as expected. Nevertheless, if you are seeing a different behavior please send us a runnable sample where the issue is replicated so we can examine it locally.
Regards,
Viktor Tachev
Progress Telerik
Hi Viktor,
Apologies for the delay, please find attached sample project.
Hi Viktor,
Apologies for the delay, please find attached sample project.
For your convenience I have prepared a sample project where the behavior is implemented. Give it a try and let me know how it works on your end. Also, you can see the behavior observed on my end in the video below:
Regards,
Viktor Tachev
Progress Telerik
Hi Viktor,
Thanks for your efforts, however I've noticed that in the sample project (and as well as in the video), you are receiving a string type in Controller method, whereas I would really like to stick to the complex object type. Can I confirm, it is not possible?
If the object passed from the client has the same signature and property names as the Model it will be parsed automatically by MVC. The relevant code would look like this:
function additionalData(e) { return { complexItem: { 'field1': 'sample value', 'field2': 'some other value' } };}Read action:
public ActionResult GetPersons([DataSourceRequest] DataSourceRequest dsRequest, Test complexItem){ var result = persons.ToDataSourceResult(dsRequest); return Json(result);}Model definition:
public class Test{ public string Field1 { get; set; } public string Field2 { get; set; }}Additionally I am attaching an updated sample. Give it a try and let me know how it works for you.
Regards,
Viktor Tachev
Progress Telerik
