Since you didn't register a specific ModelBinder
for the JqGridColumn
type, the DefaultModelBinder
will be used. But:
It won't bind Fields, only public Properties.
The expected format for Array binding is
columns[0].name
while you're actually postingcolumns[0][name]
.
The problem could be solved easily if you'll simply send your columns
in JSON format instead of Name-Value-Pairs:
$.ajax({
url: '/MyController/MyAction',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({ columns: columns })
});
Yet, if you don't like to change your class, you could register a ModelBinder
specific for JqGridColumn
and have it working even with Fields and current jQuery serialization:
public class JqGridColumnBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
string name = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[name]").AttemptedValue;
string source = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[source]").AttemptedValue;
int width = (int)bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[width]").ConvertTo(typeof(int));
bool hidden = (bool)bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[hidden]").ConvertTo(typeof(bool));
return new JqGridColumn
{
name = name,
source = source,
width = width,
hidden = hidden
};
}
}
Then register it in App_Start/ModelBindingConfig.cs:
binders.Add(typeof(JqGridColumn), new JqGridColumnBinder());