簡體   English   中英

Ajax jQuery向Mvc4控制器發送Null值

[英]Ajax jquery sending Null value to Mvc4 controller

我有一個與在堆棧溢出中搜索它的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值傳遞給控制器

有幾個問題需要解決

有什么需要

JsonRequestBehavior.AllowGet

當您的action類型為post

如果您使用的是asp.net mvc4使用Url.Action指定url,即

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

現在談論您的問題。

您的參數名稱必須匹配,將dataJson更改為contactNumber可以使用,但在傳遞單個字符串參數時無需使用JSON.stringify

[HttpPost]
        public JsonResult messages(string contactNumber) 
        {

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

您好,對於您通過Ajax調用傳遞的對象,您string dataJson將操作中的參數string dataJson的名稱更改為contactNumber

[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);
    } 

如果要在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM