My Jquery code creates three object and then places them in an other object which is posted to the action in the controller with JSON.stringify.
The string in the action looks like this:
{"sources":{"0":{"Name":"aaaaa","IP":"1.1.1.1","Username":"vvvv"},"1":{"Name":"bbbb","IP":"2.2.2.2","Username":"fdfdfdf"}},"destinations":{"0":{"Name":"aaaaa","IP":"1.1.1.1"},"1":{"Name":"bbbb","IP":"2.2.2.2"}},"protocols":{"0":{"Name":"dsfsdfsdf","Type":"FTP,SSH","Port":"22,33"}},"remarks":"sdfsdfsdf"}
So the three objects are:
var sources = {};
var destinations = {};
var protocols = {};
filled with:
sources[iS] = { 'Name': field1, 'IP': field2, 'Username': field3 };
(iS is a counter)
destinations[iD] = { 'Name': field1, 'IP': field2 };
(iD is a counter)
protocols[iP] = { 'Name': field1, 'Type': field2, 'Port': field3 }
(iP is a counter)
They are put together with:
var remarks = $('#txtRemarks').val();
var postdata = { "sources": sources, "destinations": destinations, "protocols": protocols, "remarks": remarks };
and then posted with:
$.ajax({
url: ThisController + '/AddRule',
type: 'POST',
data: 'postdata='+JSON.stringify(postdata),
dataType: 'json',
traditional: true,
success: function (data) {
$('#pnDataCollection').html(data);
});
and received with:
[HttpPost]
public ActionResult AddRule(string postdata)
{
return HttpNotFound();
}
I have three classes:
public class Source
{
public string Name { get; set; }
public string IP { get; set; }
public string UserName { get; set; }
}
public class Destination
{
public string Name { get; set; }
public string IP { get; set; }
}
public class Protocol
{
public string Name { get; set; }
public string Type { get; set; }
public string Port { get; set; }
}
How can I get the three objects in the JSON string to something like:
List<Source> = DoSomeThingWith(JsonString for Sources)
List<Destination> = DoSomeThingWith(JsonString for Destinations)
List<Protocol> = DoSomeThingWith(JsonString for Protocols)
??
IEnumerable<Source>then you do need an array -[{"Name":"aaaaa","IP":"1.1.1.1" ...}, {"Name":"bbbb","IP":"2.2.2.2"}]etc