0

Below is an Ajax call I make to a controller whenever a user clicks on a certain icon on my ASP MVC3 View. It is supposed to call a method in the controller by the name ofPopDetails, however after setting a breakpoint in the controller I can see this is not happening. I tried using both URLs listed below, however neither has worked. Since this is the first time I've used Ajax, I'm not really sure what's going on. Any ideas or suggestions would be greatly appreciated!

Ajax code from View:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script type="text/javascript">
    function GetProgramDetails(pgmname) {

        var request = $.ajax({
            type: 'POST',
            url: '~/BatchProgramsContoller/PopDetails',
            //url: '~/BatchProgramsContoller/PopDetails',
            data: { programName: pgmname },
            dataType: 'html'
        });

        request.done(function (data) {
            $('#programExplanation').html(data);
        });
    }
</script>

Method from BatchProgramsController:

    [HttpPost]
    public string PopDetails(string programName)
    {
        BatchPrograms batchprograms = db.BatchPrograms.Find(programName);
        if (batchprograms == null) return string.Empty;
        StringBuilder s = new StringBuilder();
        s.Append(batchprograms.ProgramName + " - " + batchprograms.ShortDescription);
        s.Append("<br />Job Names: " + batchprograms.PrdJobName + ", " + batchprograms.QuaJobName );
        s.Append("<br /> " + batchprograms.Description);
        return s.ToString();
    }
3
  • try dropping the ~ and just use /BatchProgramsContoller/PopDetails Commented Feb 13, 2013 at 18:16
  • can you put a break point on the ajax call? does it get called? Commented Feb 13, 2013 at 18:23
  • Yes. I used Chrome's Developer Console, set a couple breakpoints and saw that the Ajax was getting called. Commented Feb 13, 2013 at 18:27

2 Answers 2

4
url: '~/BatchProgramsContoller/PopDetails',

should be changed to

url: '~/BatchPrograms/PopDetails',

or more likely

url: '/BatchPrograms/PopDetails',
Sign up to request clarification or add additional context in comments.

Comments

1

It is generally considered bad practice in .net mvc to hard code urls as done in the answer above.

I would use

url: '@Url.Action("BatchPrograms", "PopDetails")',

This way if your routes change for any reason the mvc url helper takes care of it.

1 Comment

that's true Rik. But I think Neal already knows that. While it's bad practice to use hard url's, it's sadly common for developers to use hard urls while developing annoying syntax-unforgiving-javascript code.

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.