0

I have a control that emits a JSON array that I need to convert into a list on my Model on the controller, and trying to find a generic, scalable way of doing this. I'm assuming there is something simple and obvious.

So I get an array like this:

{ 
  { AddressId: 1, HouseNumber: 1, Street: "Any Street" },
  { AddressId: 2, HouseNumber: 2, Street: "Any Street" },
  { AddressId: 3, HouseNumber: 5, Street: "Any Street" },
  { AddressId: 4, HouseNumber: 10, Street: "Any Street" },
  { AddressId: 5, HouseNumber: 1536, Street: "Any Street" }
}

The Model:

public class AddressListModel {
   public string AnotherFormInput { get; set; }
   ...
   // A selection of other form input values that are on the page.
   ...
   public IList<AddressViewModel> Addresses { get; set; }
}

public class AddressViewModel { 
   public int AddressId { get; set; }
   public int HouseNumber { get; set; }
   public int Street { get; set; }
}

So the question is, if I already have a lot of form parameters in this form, and I want to then take this JSON array that I gather via Javascript prior to the Form post, how can I think append that array as a standard form parameter.

This isn't being done via Ajax, just a standard Form post.

Martin
  • 2,180
  • 4
  • 21
  • 41
  • 1
    The item listed under *"I get an array like this"* Is not an array-- it's an object (a malformed one at that). – Andrew Whitaker Dec 11 '13 at 14:29
  • if you want to do it not as part of the form post, and you don't want a full postback, you are going to need to use ajax. – Maess Dec 11 '13 at 14:38
  • @AndrewWhitaker You got the point, so apologies for the terminology mistake. – Martin Dec 11 '13 at 15:33
  • @Maess I need it to be a Full page post, ajax would be easy – Martin Dec 11 '13 at 15:34
  • Then you are going to need two forms on your view. The action to which you post the addresses needs return you to your full view. Its not going to be very nice from a usability standpoint. – Maess Dec 11 '13 at 15:37
  • 1
    @Martin: I guess I'm saying that in order to model bind what your control returns you're going to have to fix the JSON it's outputting (probably just by replacing the leading and ending braces with square brackets to make it an array) – Andrew Whitaker Dec 11 '13 at 16:05

2 Answers2

0

Use JSON.stringify() to turn your object into a string, then plug it into a hidden input parameter to the form. Supported in major browsers; see this, alternately, include json2.js.

You obviously need to use a JSON parser on the server side; e.g. check out the Java section at json.org.

Community
  • 1
  • 1
manuelz
  • 142
  • 4
  • The question is then how to have it done seamlessly on the server side. I can get it in a parameter, but I need the list to be seamlessly deserialized into the List Object. – Martin Dec 11 '13 at 15:35
  • As mentioned, you need a JSON parser. Check out the Java section at [json.org](http://json.org) – manuelz Dec 11 '13 at 21:25
  • I'm familar with how to deserialize JSON, the question is how to make that generic in MVC – Martin Dec 12 '13 at 15:27
0

What I actually ended up doing was catching the submit on the form, then inserting the input fields into the form so they become standard parameters.

It's not an elegant way, but it works.

Martin
  • 2,180
  • 4
  • 21
  • 41