JSON数据到MVC控制器actionresult

时间:2016-02-08 22:06:16

标签: jquery arrays json asp.net-mvc

我的Jquery代码创建了三个对象,然后将它们放在另一个对象中,该对象使用JSON.stringify发布到控制器中的操作。

动作中的字符串如下所示:

{"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"}

所以这三个对象是:

var sources = {};
var destinations = {};
var protocols = {};

填写:

sources[iS] = { 'Name': field1, 'IP': field2, 'Username': field3 };

(iS是柜台)

destinations[iD] = { 'Name': field1, 'IP': field2 };

(iD是一个柜台)

protocols[iP] = { 'Name': field1, 'Type': field2, 'Port': field3 }

(iP是柜台)

将它们放在一起:

var remarks = $('#txtRemarks').val();
var postdata = { "sources": sources, "destinations": destinations, "protocols": protocols, "remarks": remarks };

然后张贴:

$.ajax({
  url: ThisController + '/AddRule',
  type: 'POST',
  data: 'postdata='+JSON.stringify(postdata),
  dataType: 'json',
  traditional: true,
  success: function (data) {
    $('#pnDataCollection').html(data);
  });

并收到:

 [HttpPost]
 public ActionResult AddRule(string postdata)  
 {

   return HttpNotFound();
 }

我有三个班级:

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; }
}

如何将JSON字符串中的三个对象变为:

List<Source> = DoSomeThingWith(JsonString for Sources)
List<Destination> = DoSomeThingWith(JsonString for Destinations)
List<Protocol> = DoSomeThingWith(JsonString for Protocols)

...

2 个答案:

答案 0 :(得分:2)

您的代码建议您要回发3个集合以及其中一个表单控件的值,以便您的控制器方法需要

 boolean equals(Object b){
     return getClass().equals(b.getClass())
              && id==getClass().cast(b).id;
 }

但是你发回的json与你需要的东西没有关系,你需要生成集合。

[HttpPost]
public ActionResult AddRule(List<Source> sources, List<Destination> destinations, List<Protocol> protocols, string remarks)

旁注:不清楚集合的价值来自哪里。假设您有表单控件来生成集合中的项目,那么您需要做的就是使用var sources = []; var destinations = []; var protocols = []; // add some objects sources.push({"Name":"aaaaa","IP":"1.1.1.1","Username":"vvvv"}); sources.push({"Name":"bbbb","IP":"2.2.2.2","Username":"fdfdfdf"}); // ditto for destinations and protocols var data = { source: sources, destinations: destinations, protocols: protocols, remarks: $('#txtRemarks').val() }; $.ajax({ url: '@Url.Action("AddRule")'; // don't hard code url's! type: 'POST', data: JSON.stringify(data), dataType: 'json', // traditional: true, // delete this contentType: "application/json; charset=utf-8", // add this success: function (data) { $('#pnDataCollection').html(data); }); 来正确序列化数据,并在假设您已正确生成视图的情况下回发到模型

答案 1 :(得分:1)

尝试将您的操作更改为:

 [HttpPost]
 public ActionResult AddRule(List<Source> sources, List<Destination> destinations, List<Protocol> protocols)  
 {

   return HttpNotFound();
 }

并将您的数据更改为: -

$.ajax({
  url: ThisController + '/AddRule',
  type: 'POST',
  data: postdata,
  dataType: 'json',
  traditional: true,
  success: function (data) {
    $('#pnDataCollection').html(data);
  }
});

$.ajax将为您处理转换对象。只需确保actions参数名称与javascript对象属性名称匹配。

然后改变

var sources = {};
var destinations = {};
var protocols = {};

var sources = [];
var destinations = [];
var protocols = [];

将对象添加到数组时,请执行

sources.push({ 'Name': field1, 'IP': field2, 'Username': field3 });