2

Yes, there are a whole bunch of posts with similar questions. I've tried to follow the answers in them, but my ajax call still isn't reaching the controller method.

controller (SalesController.cs):

[HttpGet]
public JsonResult fillVarietiesSelect(int species_id)
{
    String[] alist = {"a","b","c"};
    return Json(alist, JsonRequestBehavior.AllowGet);
}

javascript:

$('#species-select').on('change', function () {
    var species_id = $('#species-select').val();
    console.log("species id selected " + species_id);
    alert("species id selected " + species_id);
    $('#variety-select').empty();
    $.ajax({
        type: 'GET',
        url: '@Url.Action("fillVarietiesSelect", "Sales")',
        data: {species_id : species_id},
        success: function (result) {
            alert(result);
        }
    });
});

The on change event is firing, and the alert pops up with the correct data. I have a breakpoint set at the controller method, but execution doesn't seem to get there.

7
  • Do you see any error in the console or request network tab? Commented May 26, 2015 at 2:23
  • ASP.NET MVC throws so many console errors! But the last is: Empty string passed to getElementById(). jquery-1.10.2.js:188:0 GET XHR http://localhost:65244/@Url.Action(%22fillVarietiesSelect%22,%20%22Sales%22) Commented May 26, 2015 at 3:01
  • The network panel is showing a 404 error for GET @Url.Action("fillVarietiesSelect", "Sales") Commented May 26, 2015 at 3:04
  • If you make you controller action method return string then it works file. The problem is coming when you return json. try returning list<string> instead of json Commented May 26, 2015 at 3:34
  • Try changing HttpGet to HttpPost in Controller method. Also, change ajax type from type: 'GET' to type:'POST'. Commented May 26, 2015 at 3:44

3 Answers 3

1

try doing exactly like following

Controller:

    [HttpGet]
    public ActionResult receive2(string p)
    {
        ViewBag.name = p;
        List<string> lst = new List<string>() { p };
        return Json(lst,JsonRequestBehavior.AllowGet);

    }

Client side

      $.ajax({
            type: "GET",
            url: "Main/receive2", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: { "p": $("#txtname").val() },
             dataType:"json",
            success: function (result) {
                alert("yes");
                alert('Yay! It worked!tim' + result);
                window.location = "http://google.com";
                // Or if you are returning something

            },
            error: function (result) {
                alert('Oh no aa :(' + result[0]);
            }

        });

I have checked it's working

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

1 Comment

That did it. I think the problem may have been trying to call the val an int instead of a string, and trying to send it as an int.
1

I had the same issue, changing the parameter from int to string resolved the problem.

On the server side I had,

public string RemoveByDocumentID(int DocumentID)

changing that to

public string RemoveByDocumentID(string DocumentID)

resolved the issue.

On the same context, I have another method in the same name (overloaded method). Which was also contributing to the issue, so I made the method name unique.

Comments

0

Your 404 error is telling you that @Url.Action is not being parsed by the view engine. The @ is Razor syntax, so for this to work you need to be using a .cshtml extension for C# views or .vbhtml for VB views and be using MVC 3 or above.

1 Comment

The view is index.cshtml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.