I post $('#myForm').serializeArray() to an ASP.NET MVC (2.0) action.
serialized array looks as follows:
filters[0][name] : gemcolor
filters[0][value] : Yellow
filters[1][name] : gemcolor
filters[1][value] : Green
filters[2][name] : jcOnly
filters[2][value] : true
someOtherData : abc
I want to have that consumed in:
public ActionResult GetData(Filter filter)
class Filter {
string someOtherData;
bool jcOnly;
List<string> gemcolor;
}
I can just dig through FormCollection, but I am looking for a more elegant solution (I suspect it will involve a custom model binder).
This won’t work because the default model binder doesn’t expect the data to be formatted like this. Simply use
.serialize()instead ofserializeArray(). Example:or simplify your life by using the excellent jquery form plugin which allows you to AJAXify existing HTML forms in an elegant manner:
UPDATE:
After the explanation in your comment here’s how you could proceed:
You could use the plugin from this answer which transforms the form elements into an object understandable by the default model binder and could be aggregated with some other information:
and then simply: