简体   繁体   中英

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

There are couple of issues need to be fixed

whats the need of

JsonRequestBehavior.AllowGet

when your action type is post .

If you are using asp.net mvc4 use Url.Action to specify url ie

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.

[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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM