Ajax jquery将Null值发送给Mvc4控制器

时间:2014-03-28 06:50:01

标签: javascript jquery asp.net ajax asp.net-mvc-4

我有一个问题,在堆栈溢出时搜索了它的ajax调用请求尝试了所有相关的帮助,但我无法解决问题。问题是我使用此代码从我的视图请求控制器。

<script type="text/javascript">

    $(document).ready(function () {

        $('#contactDiv ').click(function() {

           var  number = $(this).find('.ContactNumber').text();

            var dataJson = {"contactNumber": number};

            $.ajax({
                type: "POST",
                url: "../contactWeb/messages",
               data: JSON.stringify(dataJson),
               //data: dataJson,
                 //contentType: "application/json",
                  contentType: "application/json",
                cache: false,
                success: function (msg) {
                    //msg for success and error.....
                    alert(msg);
                    return true;
                }
            });

        });

    });

</script>

并且接收呼叫的控制器是

      [HttpPost]
        public JsonResult messages(string dataJson) 
        {

            Int64 userID = Convert.ToInt64(Session["userId"]);
            try
            {
                List<MessagesModel> messagesModel = new List<MessagesModel>();
                IMessages MessageObject = new MessagesBLO();

                messagesModel = MessageObject.GetAllMessagesWeb(userID , dataJson);

                //ViewData["Data"] = messagesModel;



            }
            catch (Exception e)
            {

            }

            //return View();

            string msg = "Error while Uploading....";
            return Json(msg, JsonRequestBehavior.AllowGet);
        } 

但它将NULL值传递给控制器​​

3 个答案:

答案 0 :(得分:2)

有几个问题需要修复

是否需要

JsonRequestBehavior.AllowGet

当您的action类型为post时。

如果您使用asp.net mvc4使用Url.Action指定网址即

url:"@Url.Action("ActionName","ControllerName")"

现在谈谈你的问题。

您的参数名称必须匹配,将dataJson更改为contactNumber。虽然可以使用,但在传递单个字符串参数时不需要使用JSON.stringify

[HttpPost]
        public JsonResult messages(string contactNumber) 
        {

            Int64 userID = Convert.ToInt64(Session["userId"]);

答案 1 :(得分:0)

您可以将操作中参数string dataJson的名称更改为contactNumber关于通过Ajax调用传递的对象

[HttpPost]
    public JsonResult messages(string contactNumber) //here
    {

        Int64 userID = Convert.ToInt64(Session["userId"]);
        try
        {
            List<MessagesModel> messagesModel = new List<MessagesModel>();
            IMessages MessageObject = new MessagesBLO();

            messagesModel = MessageObject.GetAllMessagesWeb(userID , contactNumber); //and here

            //ViewData["Data"] = messagesModel;



        }
        catch (Exception e)
        {

        }

        //return View();

        string msg = "Error while Uploading....";
        return Json(msg, JsonRequestBehavior.AllowGet);
    } 

答案 2 :(得分:-4)

如果你想在messages()中获取JSON,请尝试:

<script type="text/javascript">

    $(document).ready(function () {

        $('#contactDiv ').click(function() {

           var  number = $(this).find('.ContactNumber').text();

            var data = {"contactNumber": number};
            var dataJson = JSON.stringify(data);
            $.ajax({
                type: "POST",
                url: "../contactWeb/messages",
               dataType: 'text',
               data: "dataJson=" + dataJson,
               //data: dataJson,
                 //contentType: "application/json",
                cache: false,
                success: function (msg) {
                    //msg for success and error.....
                    alert(msg);
                    return true;
                }
            });

        });

    });

</script>