jquery - JSON 数据到 MVC Controller 操作结果

标签 jquery arrays json asp.net-mvc

我的 Jquery 代码创建了三个对象,然后将它们放入另一个对象中,该对象通过 JSON.stringify 发布到 Controller 中的操作。

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

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

??

最佳答案

您的代码建议您回发 3 个集合以及其中一个表单控件的值,因此您的 Controller 方法需要是

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

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

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

旁注:尚不清楚您的收藏的值(value)来自何处。假设您有表单控件来生成集合中的项目,那么您需要做的就是使用 $('form').serialize(); 正确序列化您的数据并回发到模型,假设您已正确生成 View

关于jquery - JSON 数据到 MVC Controller 操作结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35280274/

相关文章:

javascript - 使用 jQuery 以数组或 csv 形式获取所有已检查输入的 rel 属性值

javascript - 将事件加载到 FullCalendar

java - 如何在Java中通过流添加字符串

php - 使用 PHP 显示 JSON 数据

javascript - .string之后的所有字符如何大写。像手机键盘

javascript - 如何返回两个包含字符串的数组之间的差异

javascript - 仅在每个时间间隔发生 jQuery 事件

c++ - 如何检查数组中的值

jquery - 使用Jquery,点击了哪个<a>标签

php - 录音php和jQuery