简体   繁体   English

Ajax jQuery向Mvc4控制器发送Null值

[英]Ajax jquery sending Null value to Mvc4 controller

I have a problem related the ajax call request searched for it on stack overflow tried all the related help that i got but can't solve the problem. 我有一个与在堆栈溢出中搜索它的ajax调用请求有关的问题,尝试了我得到的所有相关帮助,但无法解决问题。 the problem is that i request to a controller from my view using this code. 问题是我使用此代码从我的视图请求控制器。

<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>

and the controller that receives the call is 而接听电话的控制器是

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

but it passes NULL value to the controller 但它将NULL值传递给控制器

There are couple of issues need to be fixed 有几个问题需要解决

whats the need of 有什么需要

JsonRequestBehavior.AllowGet

when your action type is post . 当您的action类型为post

If you are using asp.net mvc4 use Url.Action to specify url ie 如果您使用的是asp.net mvc4使用Url.Action指定url,即

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

Now Talking about your issue. 现在谈论您的问题。

Your parameter names must match, change dataJson to contactNumber .Though its ok to use there isnt any need to use JSON.stringify as you are passing single string parameter. 您的参数名称必须匹配,将dataJson更改为contactNumber可以使用,但在传递单个字符串参数时无需使用JSON.stringify

[HttpPost]
        public JsonResult messages(string contactNumber) 
        {

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

Hi could you change the name of your parameters string dataJson in your action to contactNumber in respect to the object you pass via your Ajax call 您好,对于您通过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);
    } 

If you want to get JSON in messages() try this: 如果要在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