Model:
public class Product { public int ProductID { get; set; } public string Name { get; set; } public string ProductNumber { get; set; }
public List<ProductLocations> Locations { get; set; } }
public class ProductLocations { public string Address { get; set; } }
Controller
[GridDataSourceAction] [HttpPost] public ActionResult PostDataMethod3(Product Param1, string Param2, string Param3) { try { List<Product> products = new List<Product>();
products.Add(new Product { ProductID = 1, Name = "Product 1", ProductNumber = "12345" }); products.Add(new Product { ProductID = 2, Name = "Product 2", ProductNumber = "12346" }); products.Add(new Product { ProductID = 3, Name = "Product 3", ProductNumber = "12347" }); products.Add(new Product { ProductID = 4, Name = "Product 4", ProductNumber = "12348" }); products.Add(new Product { ProductID = 5, Name = "Product 5", ProductNumber = "12349" });
return View(products.AsQueryable()); } catch (Exception ex) { string error = ex.Message; return null; } }
AJAX call
var appJson = { "Param1": { "ProductID": 1, "Name": "test product", "ProductNumber": "P123", "Locations": [{ "Address": "Address1" }, { "Address": "Address2" }]}, "Param2": "Param2", "Param3": "Param3" };
$('#postData3').click(function (e) { e.preventDefault();
$.ajax({ url: '@(Url.Action("PostDataMethod3"))', type: 'POST', traditional: true, contentType: "application/json", dataType: "json", data: JSON.stringify(appJson), success: function (response) { alert('Succcess'); $('#grid').igGrid({ dataSource: response }).igGrid("dataBind"); }, error: function (xhr, ajaxOptions, thrownError) { alert('Failure - ' + xhr.responseText); $('#grid').igGrid({ dataSource: null }).igGrid("dataBind"); } }); });
Eroor:
2022infragisticerror.igGridQueryStringToJson.zip
Hello Anitha,
Thank you for the provided sample!
I’ve been looking into your question and after an investigation what I could say is that the initial error is thrown because the “appJson” variable was not being parsed properly. It can be passed to the “data” property without being stringified. Another issue I have found is that the “PostDataMethod3” never receives its parameters even if the grid loads, so changing the code in the following way provides the desired results:
var appJson = {
Param1: { "ProductID": 1, "Name": "test product", "ProductNumber": "P123", "Locations": [{ "Address": "Address1" }] },
Param2: { "ProductID": 2, "Name": "test product", "ProductNumber": "P123", "Locations": [{ "Address": "Address1" }] },
Param3: { "ProductID": 3, "Name": "test product", "ProductNumber": "P123", "Locations": [{ "Address": "Address1" }] }
}
…
$.ajax({
url: '@(Url.Action("PostDataMethod3"))',
type: 'POST',
data: appJson,
success: function (response) {
alert("Success");
$('#grid').igGrid({ dataSource: response }).igGrid("dataBind");
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failure - ' + xhr.responseText);
$('#grid').igGrid({ dataSource: null }).igGrid("dataBind");
});
Attached could be found my sample for your reference. Please test it on your side and let me know how it behaves.
Regards, Aleksandar Atanasov,
Infragistics.
Index.zip
Can you check this as priority
Hello Axl Atanasov,
Do you any fix for this.
Thank you for answering my questions!
I am currently investigating the cause of this behavior. I will keep you posted on my progress, and I will get back to you soon with more information or questions for you.
Please feel free to continue sending updates to this case at any time.
Regards, Aleksandar Atanasov, Infragistics
I have done some work around this. It an issue with Infragistics.Web.Mvc, Version=5.22.2.7.
GridDataSourceActionAttribute throwing exception incase of request/response has array object.
If you can get solution for this that would be great.
After further investigation I was able to discover that the GridDataSourceAction attribute is used when filtering or paging is handled remotely by the server and it is not intended for a post request to the server. In order to achieve the desired behavior, what I could suggest is to remove the attribute and to convert the result in the PostDataMethod3 to json result object as below:
public ActionResult PostDataMethod3(Product Param1)
{
try
List<Product> products = new List<Product>();
products.Add(new Product { ProductID = 7, Name = "Product 1", ProductNumber = "12345" });
products.Add(new Product { ProductID = 2, Name = "Product 2", ProductNumber = "12346" });
products.Add(new Product { ProductID = 3, Name = "Product 3", ProductNumber = "12347" });
products.Add(new Product { ProductID = 4, Name = "Product 4", ProductNumber = "12348" });
products.Add(new Product { ProductID = 5, Name = "Product 5", ProductNumber = "12349" });
JsonResult result = new JsonResult();
result.Data = products;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
Please test it on your side and let me know how it behaves.
No, it's not a good solution to remove [GridDataSourceAction]. my application is so big it full depended on [GridDataSourceAction] on lots of screens .. even your are converting JsonResultset. All my application using return View(products.AsQueryable());
I am expecting solution GridDataSourceAction why it's throwing exception if my payload has List or Array.You can could find path for GridDataSourceAction that helps a lot for all user who trying to to use Infragistics.Web.Mvc, Version=5.22.2.7.
Taking [GridDataSourceAction] off the table is not a good idea. You are converting JsonResultset, however my application is massive and relies on [GridDataSourceAction] on a ton of screens. Whenever I call products.AsQueryable(), every one of my apps returns a View. geometry dash
After an investigation, I have determined that the GridDataSourceAction attribute intended use is when the request is made directly from the grid using a ‘GET’ request. Using an external ‘POST’ ajax call like in the example provided the GridDataSourceAction throws an exception. What I can suggest is to create a duplicate method to be called when $.ajax is used that does not use the attribute but instead returns a JsonResult.
Thank you for using Infragistics components.
Regards, Aleksandar Atanasov, Infragistics.