3

I've been implementing a custom model binder to handle the data returned from the datatables.net jQuery plugin but am having trouble actually getting to the data as it is being passed back in the request body and my ModelBindingContext only seems to have a ValueProvider for the query string.

Currently my controller method signature looks like this:

[HttpPost]
public DataTablesResult GetTableData([ModelBinder(typeof(MyCustomModelBinder))] DataTablesInput input)

which is getting data from a simple $.post().

I've tried adding a FromBody attribute to the input parameter, but that seems to stop the custom model binder being called at all.

How do I get my model binder to recognise data in the request body, or if that's not possible, how do I move my posted data into the query string (I'd like to not do this!)?

Mourndark
  • 2,526
  • 5
  • 28
  • 52
  • How are you sending the data? – Andrei Jan 13 '14 at 17:03
  • Do you have the json you receive from the POST? What exactly do you do with the custom binder? By default, it should map json automatically if you provide a model that implements the json structure. If find it weird you are using WebApi with DataTables.Net, a web service should be data agnostic and not rely on UI side structures like DataTables.Net and such. – Karhgath Jan 13 '14 at 17:44
  • Yes, I have the POST json, it's just it's in a part of the request that the regular model binder can't access. I'm trying to find an easily repeatable solution for this, instead of parsing the (very large) json string every time I want a table on a page. – Mourndark Jan 13 '14 at 23:58
  • What do you suggest I use if not WebAPI for a non-agnostic JSON source? – Mourndark Jan 14 '14 at 09:46
  • Here's a simple one-liner that gives you the request body: `var body = actionContext.Request.Content.ReadAsStringAsync().Result;` - you can invoke that from within your custom modelbinder. – Lasse Christiansen May 23 '14 at 16:13

0 Answers0