Ajax调用向控制器发送空值我是否正确发送值? [英] Ajax call sending null value to controller am I sending the values correctly ?

查看:89
本文介绍了Ajax调用向控制器发送空值我是否正确发送值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在调试器中看到数据存储在数组中但是当我向控制器发送数据时obj为null可以有人建议我哪里出错了吗?



我的尝试:



i can see in the debugger that data is stored in the arrays but when i am sending data to controller the obj is null can someone suggests where am i going wrong ?

What I have tried:

     my model: 

        public class Hello
    {
        public List<string> name;
        public List<string> phone;
        public List<string> contact;
    
    }

my controller code is 
        
      public ActionResult Home(Hello obj) // obj is coming out to be null
        {
                 
        }


my script is 

         var names =[];
         var phones =[];
         var contacts = [];
           
         // some code to fill the arrays 

        var obj = JSON.stringify({
                name: names,
                phone: phones,
                contact: contacts,
              });
            debugger;
            $.ajax({
                cache: false,
                url: 'Home',
                data: { obj:obj },
                success: function (data) {
                    var response = JSON.parse(data);
                    window.location = 'Download?fileGuid=' + response.FileGuid
                                      + '&filename=' + response.FileName;
                }
            })

推荐答案

.ajax({
cache: false
url:' Home'
数据:{obj:obj},
success:function(data){
var response = JSON.parse(data);
window.location = ' 下载?fileGuid =' + response.FileGuid
+ ' & filename =' + response.FileName;
}
})
.ajax({ cache: false, url: 'Home', data: { obj:obj }, success: function (data) { var response = JSON.parse(data); window.location = 'Download?fileGuid=' + response.FileGuid + '&filename=' + response.FileName; } })


试试这个



try this

public ActionResult Home(string obj)
     {
         JavaScriptSerializer js = new JavaScriptSerializer();
         Hello objHello = js.Deserialize<Hello>(obj);
     }





添加此引用



add this reference

using System.Web.Script.Serialization 


尝试移动 JSON.stringify 调用:

Try moving the JSON.stringify call:
var obj = {
    name: names,
    phone: phones,
    contact: contacts,
});


这篇关于Ajax调用向控制器发送空值我是否正确发送值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆