0

Using model binding, ASP.NET MVC can take FormData from a form with repeating elements, signified by the use of indexes in square brackets, and populate a .NET object.

So this:

MyItem[0].MyId=1321&MyItem[1].MyId=4324

can be bound to a class MyItems containing a list of MyItem, populating two rows, using the [FromForm] attribute in the controller.

However I can't find a way to do this manually. If .NET can do it, I assumed that I'd be able to have a string containing the FormData example above, and populate an object on demand:

MyItems myItems = BindModelFromFormData_OrSomethingLikeThat<MyItems>(formDataString);

There are many ways to deserialise JSON and so on into C# classes. Is there a way to do that with indexed FormData, which is completely 'flat' but where the index numbers signify arrays, using whatever methods model binding already uses?

4
  • Is this for legacy NET Framework (4.x) or for new .NET Core (3 / 5-9) ? Commented Aug 13 at 21:46
  • Request.Form is a FormCollection type which is a collection of name value pairs, acting much like a Dictionary. What exactly are you trying to achieve over just letting MVC build the model? Commented Aug 13 at 21:58
  • It sounds like they want all that model binding has to offer, but then outside of a Controller class and with a string to simulate the Form payload. Commented Aug 13 at 22:05
  • Yes it's exactly that, model binding features but with no controller. Reasons rather complex - have a mixed WebForms/MVC app., with an MVC page in an IFrame over a WebForms page - want to capture the FormData from the IFrame using Javascript, store it on the parent WebForms page and process it later after the parent page has posted back. There are plenty of other ways around this messy problem, but if .NET can deserialize FormData nicely in a controller, my hope was that it can deserialize it anywhere :) Commented Aug 14 at 7:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.