【问题标题】:How to pass an object to controller using ajax call如何使用ajax调用将对象传递给控制器
【发布时间】:2013-04-29 04:53:43
【问题描述】:

我想将一个对象传递给控制器​​并检索控制器中的值。 我的定义如下:

HTML代码:

 var positionarray = [];

Javascript:

 $("#button").live('click',function(){
     positionarray.push({
         id: sessionStorage.getItem('id'),
         value: $("#input").val() 
     });
 });

 // on save button click
 $.ajax({
       type: "GET",                                                 
       url:"/Bugs/Position",                                                 
       data: {
           array:positionarray      
       },
       cache: false,
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (json) {

       }
 });

但我无法检索控制器中的值。它正在为空。

【问题讨论】:

  • 你的动作看起来怎么样?
  • /Bugs/Position 页面打印你的Request,然后检查你得到了什么,我认为它是正确的。
  • 我可以传递给控制器​​,但是控制器中的值是空的
  • @Naidu 你在传递一个数组,你可以用 Post 而不是 GET

标签: jquery ajax asp.net-mvc


【解决方案1】:

试试这个:- 你正在传递一个对象数组,所以你应该做 HTTPPost 而不是 HttpGet(这将适用于原始类型数组,比如 int、strgin 等列表)通过查询字符串发送它(记住查询的限制细绳)。 用 HTTPPost 试试这个

 $.ajax({
       type: "POST",                     
       url:"Home/Position",                                                 
       data: JSON.stringify({
              array: positionarray
       }),
       cache: false,
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (json) {

       }

[HTTPPost]
public void Position(YourClass[] array){...

【讨论】:

    【解决方案2】:

    试试这个:

    $.ajax({
           type: "GET",                                                 
           url:"/Bugs/Position",                                                 
           data: 'array='+positionarray      
           cache: false,
           dataType: "json",
           contentType: "application/json; charset=utf-8",
           success: function (json) {
    
           }
    });
    

    【讨论】:

    • 将其作为字符串而不是对象传递。我有一种感觉,鉴于您的数组是 JSON 类型,它的数据对象格式不正确。
    【解决方案3】:

    试试这个:

    您的ajax call 必须在按钮单击功能中才能使用,

    在您的代码中,您的 ajax call 在您点击之前运行,因此它将通过 null 到您的控制器

    $("#button").live('click',function(){
         positionarray.push({
             id: sessionStorage.getItem('id'),
             value: $("#input").val() 
         });
    
    
        // on save button click
        // this code must run after `button click` and after pushing data in
        // positionarray variable
        $.ajax({
               type: "GET",                                                 
               url:"/Bugs/Position",                                                 
               data: {
                   array:positionarray      
               },
               cache: false,
               dataType: "json",
               contentType: "application/json; charset=utf-8",
               success: function (json) {
    
               }
        });// here ajax function code ends
    
    });// here button click function ends
    

    【讨论】:

    • @Naidu 检查一次,在你的question你的ajax codebutton click function之外。
    • 但是我写了一个评论说ajax会在按钮点击中
    • 这是真的,但 cmets 不起作用 (code matters),在您发表评论之前,您已使用 }); 关闭了 button click function。先测试我的答案,然后输出一些结果。
    • 这里有两个按钮。在第一个按钮中,我正在添加值,在第二个按钮中,我正在保存。很抱歉我不清楚。现在问题已经解决了。 Json.stringify 可以解决问题。谢谢
    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    相关资源
    最近更新 更多