2016-05-05 48 views
2

我正在开发一个ASP.NET MVC应用程序,其中我需要将存储在sessionStorage中的数据发送到Controller。该存储器具有键值对,其中密钥是“确认的”。该值是一个包含数字的数组,如[1,2,3,4,5]。使用ajax发送大字符串到控制器

我需要发送这个数组到我的控制器,并且一切正常,除非这个数组太长。我试着改变配置有:

<appSettings> 
    <add key="aspnet:MaxJsonDeserializerMembers" value="1000000" /> 
</appSettings> 

<system.web.extensions> 
    <scripting> 
     <webServices>             
      <jsonSerialization maxJsonLength="1000000" />     
     </webServices> 
    </scripting> 
</system.web.extensions> 

,但似乎没有任何工作。只有当数组不长时才能正常工作。 这里是我的代码:

脚本:

$(".enviar").click(function() { 
     getSelectedItems(); 

     var seleccion = sessionStorage.confirmed; 

     $.ajax({ 
      url: "/Controller/Action", 
      type: "POST", 
      //data: { confirm: JSON.stringify(seleccion) }, 
      data: JSON.stringify({ confirm: seleccion }), 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      success: function (returndata) { 
       window.location = "@Url.Action("SomeAction","Controller")"; 
      }, 
      error: function (returndata) { 
       window.location.href = "@Url.Action("AnotherAction","Controller")"; 
      } 
     }); 

     //sessionStorage.clear(); 
    }); 

控制器:

[HttpPost] 
    public ActionResult Action(string confirm) 
    { 
     if (!String.IsNullOrEmpty(confirm)) 
     { 
      confirm = confirm.Substring(1, confirm.Length - 2); 
      var confirmadas = confirm.Split(','); 

      foreach (var id in confirmadas) 
      { 
       //change things in DB 
      } 

      return Json(new { ok = true, newurl = Url.Action("SomeAction", "Controller") }); 

     } 
     return Json(new { ok = false, newurl = Url.Action("SomeAction", "Controller") }); 
    } 

请帮帮忙,我已经试过几乎所有和它的作品的时候,但是其他时候它不“T。

谢谢。

回答

0

阵列为字符串1.join:

var str=seleccion .join(","); 

2.just它传递给数据对象

data: { confirm: str} 
相关问题