hi
I created a simple project with .Net Core 5 and try to use telerik grid
list data to grid ( work ) post data with ajax ( not work )
when I try to post data to controller and the controller show no data ( count=0)
where am i doing wrong
If I added a [FromBody] tag its working, but same structure working earlier version like 2, 3.
thank you
all structure like below
// Controller
public IActionResult GetData()
{
var model = new TestModel();
return View(model);
}
[HttpPost]
public IActionResult GetData([DataSourceRequest] DataSourceRequest request)
{
var query="SELECT ID,NAME,SURNAME FROM TABLE";
var dt = GetDataFromDatatable(query);
var model = (from DataRow dr in dt.Rows
select new TestModel
{
ID = Convert.ToInt32(dr[0]),
NAME = dr[1].ToString(),
SURNAME = dr[2].ToString()
}).ToList();
return new JsonResult(model.OrderBy(x => x.NAME).ToDataSourceResult(request), new Newtonsoft.Json.JsonSerializerSettings());
}
public JsonResult SendData(List<TestModel> modelList)
{
//Do Semething.
return Json(new { data = modelList }, new Newtonsoft.Json.JsonSerializerSettings());
}
// View
<button id="btnSend">Send</button>
@(Html.Kendo().Grid<TestModel>()
.Name("gridTest")
.AutoBind(true)
.Columns(columns =>
{
columns.Bound(p => p.ID).Width(50);
columns.Bound(p => p.NAME).Width(100);
columns.Bound(p => p.SURNAME).Width(100);
})
.DataSource(source => source
.Ajax().ServerOperation(false)
.Model(model =>
{
model.Id(p => p.ID);
})
.PageSize(17)
.Read(read => read.Action("GetData", "Tests"))
)
)
)
)
// Ajax Call
$(document).ready(function () {
$("#btnSend").click(function (e) {
e.preventDefault();
var modelList = [];
$.each($(".k-grid-content tbody tr"), function () {
var grid = $('#gridTest').data('kendoGrid');
var selectedRow = grid.dataItem(this);
modelList.push(selectedRow);
});
console.log(modelList);
$.ajax({
type: "POST",
url: '/Tests/SendData',
data: JSON.stringify({modelList}),
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
},
error: function (data) {
}
});
});
});