This is a migrated thread and some comments may be shown as answers.

Passing complex object as parameter to mvc controller

11 Answers 3289 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Divyang
Top achievements
Rank 1
Divyang asked on 11 Mar 2019, 11:08 AM

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

Sort by
0
Viktor Tachev
Telerik team
answered on 13 Mar 2019, 10:54 AM
Hello Divyang,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Divyang
Top achievements
Rank 1
answered on 13 Mar 2019, 11:08 AM

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 };
};
0
Viktor Tachev
Telerik team
answered on 15 Mar 2019, 08:07 AM
Hi,


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);

 
Give the approach a try and let me know how it works for you.


Regards,
Viktor Tachev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Divyang
Top achievements
Rank 1
answered on 16 Mar 2019, 12:18 PM

Hi Viktor,

I've tried above approach but it only works if I change my controller method's signature to accept a dynamic object?

0
Viktor Tachev
Telerik team
answered on 19 Mar 2019, 02:34 PM
Hi Divyang,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Divyang
Top achievements
Rank 1
answered on 27 Mar 2019, 04:47 PM

Hi Viktor,

Apologies for the delay, please find attached sample project.

0
Divyang
Top achievements
Rank 1
answered on 27 Mar 2019, 04:49 PM

Hi Viktor,

Apologies for the delay, please find attached sample project.

0
Divyang
Top achievements
Rank 1
answered on 27 Mar 2019, 05:05 PM
I am not able to upload the file but you can reproduce it by creating a new project in visual studio using the MVC template and by preparing a model, view and controller method as per my original post. In the new project I am trying to upload has the exact same thing without anything extra at all. 
0
Viktor Tachev
Telerik team
answered on 29 Mar 2019, 01:49 PM
Hi Divyang,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Divyang
Top achievements
Rank 1
answered on 01 Apr 2019, 10:18 AM

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?

0
Viktor Tachev
Telerik team
answered on 03 Apr 2019, 08:23 AM
Hi Divyang,

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
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Divyang
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Divyang
Top achievements
Rank 1
Share this question
or