weixin_33739541 2017-04-11 17:51 采纳率: 0%
浏览 42

Ajax发布请求MVC

I have this JQuery function:

function EditContactPost()
    {
        var urlEditPost = $("#hdnInfo").data("url_add_contact");
        var test = $("#formEditContact").serialize();        
        alert(test);
        $.ajax({
            type: 'post',
            url: urlEditPost,                    
            data:{ marketingContactVM: test },                           
        })
    }

I'm doing an alert as you see and I'm watching that the test variable has the form serialized correctly with all the values. But when the controller action receives this request on server side marketingContactVM is always null. Why is this happening? My controller action looks like this:

 [HttpPost]
        public ActionResult AddContact(MarketingVM marketingContactVM) //always null
        {
some code...
        }

Just in case this is my model class, it contains another list inside:

     public class MarketingVM
        {
            public IEnumerable<string> States { get; set; }  
            public List<MarketingVM> MarketingList { get; set; }           
            public string Firstname { get; set; }
            public string Lastname { get; set; }
            public string Company { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public string Country { get; set; }
            public string Phone { get; set; }
            public string Fax { get; set; }
            public string Mail{ get; set; }
            public string URL { get; set; }
            public bool IsTrue{ get; set; }
            public int OtherId{ get; set; }
    }

I've checked the Network tab in DevTools and this is the Request URL:http://localhost:56602/ControllerName/AddContact , that should be fine. But the data is being sent as a FormData, like this:

    marketingContactVM:Company=Microsoft+Company&Email=user10%40email.co&First=
Auto&Last=Mapper&Phone=98797988997&Fax=&URL=ww.auto.org&Adress=automapper+
street&City=auto+city&State=FL&Zip=33654&Country=USA&MarketingContacts%5B0%5D
.Name=1%2F03%2F2012+1%3A15+PM&MarketingContacts%5B0%5D.IsSelected=false

.. and like that. Maybe the way the data is being sent.. I think that the correct format should be:

marketingContactVM: Company=Microsoft Company Email=user10@email.co First=Charles Last=Johnston Phone=98797988997 Fax=989898 URL=ww.auto.org Adress=North streetCity=Sacramento State=FL Zip=33654 Country=USA MarketingContacts.Name=Piotr MarketingContacts.IsSelected=false

  • 写回答

3条回答 默认 最新

  • weixin_33737134 2017-04-11 17:57
    关注

    Try this:

    $.ajax({
      url: urlEditPost,
      method: "POST",
      data: { marketingContactVM: test },
      dataType: "json"
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题