簡體   English   中英

JSON數據到MVC控制器actionresult

[英]JSON data to MVC controller actionresult

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

(是櫃台)

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個集合以及其中一個表單控件的值,以便您的控制器方法需要

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

旁注:不清楚集合的價值來自哪里。 假設您有表單控件來生成集合中的項目,那么您需要做的就是使用$('form').serialize(); 假設您已正確生成視圖,正確序列化數據並回發到模型

嘗試將您的操作更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM