3

using the following script, I am trying to access the variables being sent using data in the ajax function but I couldn't.

<script>
$('#inline-username').click(function () {
    var comments = $('#inline-username').val();
    //var selectedId = $('#hdnSelectedId').val();

    $.ajax({
        url: '@Url.Action("UpdateOrder")', // to get the right path to controller from TableRoutes of Asp.Net MVC
        dataType: "json", //to work with json format
        type: "POST", //to do a post request
        contentType: 'application/json; charset=utf-8', //define a contentType of your request
        cache: false, //avoid caching results
        data: { test: $(this).text() }, // here you can pass arguments to your request if you need
        success: function (data) {
            // data is your result from controller
            if (data.success) {
                alert(data.message);
            }
        },
        error: function (xhr) {
            alert('error');
        }
    });
});

here is the action in the controller

public ActionResult UpdateOrder()
    {
        // some code
        var test = Request.Form["test"];
        return Json(new { success = true, message = "Order updated successfully" }, JsonRequestBehavior.AllowGet);
    }

I tried Request.Form["test"] but the value of it is null. how should I receive the objects of data?

1
  • you need to specify the controller name in url: '@Url.Action("UpdateOrder","ControllerName")', and decorate your action with [HttpPost] attribute. Commented May 7, 2017 at 12:06

4 Answers 4

8

Your ActionResult is GET And you have no input parameter for your ActionResult so either change those or see below:

<script>
$('#inline-username').click(function () {
    var comments = $('#inline-username').val();
    //var selectedId = $('#hdnSelectedId').val();

    $.ajax({
        url: /ControllerName/ActionName
        dataType: "json",
        type: "GET", 
        contentType: 'application/json; charset=utf-8', //define a contentType of your request
        cache: false, 
        data: { test: comments  },
        success: function (data) {
            // data is your result from controller
            if (data.success) {
                alert(data.message);
            }
        },
        error: function (xhr) {
            alert('error');
        }
    });
});

Then within your controller:

public ActionResult UpdateOrder(string test)
    {
        // some code
        return Json(new { success = true, message = "Order updated successfully" }, JsonRequestBehavior.AllowGet);
    }

Update

Remember if you want to use POST then action you are calling has to be [HttpPost] like:

[HttpPost]
public ActionResult Example()

When there is no HTTP above your ActionResult Then Its By Default Get.

Sign up to request clarification or add additional context in comments.

1 Comment

@MohammedAlAamri Glad it did mate :)
0

The action method should be decorated with the [HttpPost] attribute. Have you used the debugger to ensure that you're actually calling in to that method?

You can always just define a view model in C# and then accept that as a parameter to your post method - Asp.MVC will parse the post data for you as long as the names of the values are the same as your model.

Comments

0

Have you marked your action method with [HttpPost] attribute. ?

Comments

0

This post helped me a lot. I did the GET but POST raise an Internal Server Error just with [HttpPost]:

[HttpPost]
public ActionResult SaveOrder(int id, string test, string test2)

So i had to set params data with JSON.stringify and it worked. My full ajax request for POST:

$.ajax({
    url: "/Home/SaveOrder",
    dataType: "json",
    type: "POST", 
    contentType: 'application/json; charset=utf-8', //define a contentType of your request
    cache: false, 
    data: JSON.stringify({ id:2, test: "test3", test2: "msj3" }),
    success: function (data) {
        // data is your result from controller
        if (data.success) {
            alert(data.message);
        }
    },
    error: function (xhr) {
        alert('error');
    }
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.