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

win.refresh with grid dataItem data

3 Answers 242 Views
Window
This is a migrated thread and some comments may be shown as answers.
Nouman
Top achievements
Rank 1
Nouman asked on 19 Sep 2017, 12:23 AM

I have a custom Edit button on Grid.

Upon clicking Edit, I get the selected grid data row:

function onUserOrgEdit(e) {

...

var data = this.dataItem($(e.currentTarget).closest("tr"));

...

}

Next, I need to refresh my window:

 var win = $("#createUserWindow").data("kendoWindow")

 win.refresh({
        url: "/Admin/EditOrgRole",
        data: data

});

 

I cannot JSON.stringify because wrong content-type is sent to controller action and asp.net core mvc not able to bind JSON correctly.  I get null values in controller action.  

What is the correct way to do this?

 

Thanks.

 

 

 

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 21 Sep 2017, 02:00 PM
Hello Nouman,

I am attaching an ASP.NET MVC project, where a similar scenario to the one described is demonstrated (Sending the currently edited Grid row model data to a controller end-point).

To achieve the desired result, I have used the edit event of the Grid, where a new object is created, which is populated with the model data of the currently clicked grid row. The an ajax request is triggered with the Window refresh() method, by also passing the new model object as a parameter:
<script>
function onEdit(e) {
  var wnd = $("#window").getKendoWindow().center().open();
  var modelData = e.model;
 
  var modelData = {
    OrderID: modelData.OrderID,
    ProjectID: modelData.ProjectID,
    Freight: modelData.Freight,
    OrderDate: modelData.OrderDate,
    ShipName: modelData.ShipName,
    ShipCity: modelData.ShipCity
  }
 
  wnd.refresh({
    url: "/Home/GetWindowData",
    data: modelData,
    dataType: "json"
  });
}
</script>

Then the data can be successfully received in the controller method with the following signature:
public JsonResult GetWindowData(OrderViewModel model)
{
}


Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Nouman
Top achievements
Rank 1
answered on 21 Sep 2017, 03:31 PM

Thanks for your response Dimitar.  This was my approach as well, but I was running into the following problem:

For Asp.net core, it seems we need FromBody attribute in our controller action in order to bind JSON correctly.  This results in interrogating the header content-type.  It is expecting application/json but win.refresh is always sending x-www-url-formencoded - even if we specify dataType: "json"

If you check in fiddler, what is the content-type header value?

In all honesty, I have not spent too much time on this issue and this is just my guess.

 

Thanks.

0
Dimitar
Telerik team
answered on 26 Sep 2017, 08:49 AM
Hello Nouman,

The contentType property tells the server that we are sending the data in JSON format. If it is not specified explicitly, the default content type will be used which is application/x-www-form-urlencoded.

In the provided solution in my previous reply, a flat view model data is being sent to the server. In this case there is no need to set the contentType, as the model binder will be able to map the form data to your view model object in the server. Also there is no need to decorate the controller method parameter with the  [FromBody] attribute.

If you have a complex object, then the contentType has to be set explicitly to "application/json". This will force the ajax request to send the JS object as the request payload, in the body of the request (in JSON format). To receive the data in the controller successfully, the parameter has to be decorated with [FromBody].

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Window
Asked by
Nouman
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Nouman
Top achievements
Rank 1
Share this question
or