Hi,
I Want to know what exactly OnActionExecuted action of GridDataSourceActionAttribute does?
we were using model class to bind data to view in our project as below
In Controller:
System.Type modelType = Type.GetType("System.Collections.Generic.List`1[" + modelClass + "]")
// here model class is public class which has all properties which we want to display in gri.d.
string json = Newtonsoft.Json.JsonConvert.SerializeObject(dataTable);var model = Newtonsoft.Json.JsonConvert.DeserializeObject(json, modelType);
return ((IList)model).AsQueryable();
after controller action gets executed the below code is getting called.
public void OnActionExecuted(ActionExecutedContext filterContext){var viewResult = filterContext.Result as ViewResult;GridDataSourceActionAttribute igAttribute = new GridDataSourceActionAttribute();igAttribute.OnActionExecuted(filterContext);}
This was working fine for us when we perform sorting and grouping with the above code.
Now we are removing the model class and binding data directly to grid as below
string json = Newtonsoft.Json.JsonConvert.SerializeObject(dataTable);var model = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(json)
with this code changes sorting and grouping is not working for me. gettinng argumentnullexception (value cannot be null).
i cannot debug the method also for more information.
Can anyone help me to figure out why this issue is happening?
Thanks,
Shwetha
Hello Swetha,
Thank you for posting in our forum.
The GridDataSourceActionAttribute class extends the MVC FilterAttrubite. The idea of this filter is to transform the ActionResult into a JsonResult instance, taking into account all Request parameters such as paging, sorting, and filtering.
The problem might be occurring because the JSON doesn’t get deserialized properly. OnActionExecuted tries to type cast the Model as a DataTable, DataSet or IQueryable using the “as” keyword. When it fails, instead of throwing an error, it returns null. That might be the reason you are getting the ArgumentNullException.
Could you please provide some clarification on why you have changed the line:
var model = Newtonsoft.Json.JsonConvert.DeserializeObject(json, modelType);
to this:
var model = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(json) ?
This might give me a clue about what is causing the issue and give you a more concise answer.
If you need any additional assistance, feel free to contact me.
Best Regards,
Vasil Pavlov
Associate Software Developer
Infragistics, Inc.
In the model class, we have defined the properties of each column which we display on the grid.
if we want to add a new column to the grid, every time we need to add a new property to that model so data table will get deserializes properly. but we do not need this going and changing the model all the time. that's why we removed model
so we replaced
var model = Newtonsoft.Json.JsonConvert.DeserializeObject<List<object>>(json)
thanks,
Hello Shwetha,
Removing the model from an MVC project is a very specific scenario and sometimes leads to unexpected issues.
As this is hard to replicate properly on my side and depends heavily on your specific implementation and initialization options, providing an isolated code sample without external dependencies would be greatly appreciated and would help me investigate the problem and get to the root of it.