I currently have 2 classes that inherit a base BaseGridRowModel, eg:
- PersonRowModel
- CityRowModel
I have created a controller which accepts the base as input, and pushes it the cache for use later.
[HttpPost]
public ActionResult RowCheckedStatusChanged(BaseGridRowModel row)
{
try
{
I then call the method, pushing the grid row to the controller in an ajax call.
$.ajax({
type: 'POST',
url: "/Central/MyGrid/RowCheckedStatusChanged",
data: rowData.toJSON(),
cache: false,
However all the extra fields are lost since are not part of the base. This always becomes null and throws an exception.
var rowData = (row as PersonRowModel);
if (rowData == null)
{
throw new Exception("Row must inherit BaseGridRowModel.");
}
I need these fields later for picking back out of the cache. Is there any way to save them?
Thanks
The problem is the model binder is binding your data to the BaseGridRowModel. Essentially it is deserializing the posted data into a BaseGridRowModel.
So the model binder isn’t smart enough to recognized the extra fields and magically determine there is a class that inherits from the one in the action and use that instead.
You would think you could create overloads for the various types if you want that data and then funnel it down into the generic version but I believe that wouldn’t work either. You could create your own custom model binder that would look for the extra data and create the correct type then effectively upcast it to BaseGridRowModel
Alternatively you could just look in HttpContext.Current.Request.Form to get the extra fields. Or you could add the fields to the method… ie…