0

I'm trying to use AJAX via jquery with ASP.NET MVC, but I'm not able to hit my controller action and I think it might be because my url is not specified correctly.

The problem is, I'm not sure what is should be!

Say I have a controller named "Home", and an action named "AjaxAction", would the jquery look like this?

$.get("Home/AjaxAction");

This doesn't seem to be working for me, so I'm wondering if there's a more correct way to do this using Html helpers or something from ASP.NET MVC.

For instance, has anyone tried doing something like this?

<a href="#" 
    onclick="doSomeFunction('<%= Url.Action("AjaxAction", "Home") %>')">
    Click Me
</a>
<script>
    function doSomeFunction(url)
    {
        $.get(url);
    }
</script>

Would that work or am I barking up the wrong tree? I've tried it myself but it's not working, but I'm not sure if it's because I'm doing something wrong or if it's because the entire approach is incorrect.

4
  • 2
    have you tried /Home/AjaxAction? Commented Sep 10, 2009 at 14:30
  • @TheVillageIdiot Thanks, but I've tried that too and I'm not getting a hit on my controller action! Commented Sep 10, 2009 at 14:41
  • Do you have an entry in your routing table that matches this route? Commented Sep 10, 2009 at 14:49
  • @Robert It's using the default convention/configuration, so yes. Commented Sep 10, 2009 at 15:28

3 Answers 3

1

Don't know if you cut/pasted wrong, but you are missing a parenthesis. Also, add single quotes around the URL.

onclick="doSomeFunction('<%= Url.Action("AjaxAction", "Home") %>')"

Give that shot.

The only other difference I have in my code is 'return' in front of doSomeFunction(), but I'm a javascript idiot so probably just cut/pasted that from someplace else, no idea if you need it.

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

2 Comments

Thanks @eyston, I'll edit my question. I didn't copy and paste, so that was just me fat fingering, but I'll check on those things just to be sure.
I was missing the closing parenthesis =P The weird thing is I wasn't getting any errors so it was hard to catch it. Gotta love javascript debugging =P
0

It should work. Mine looks a bit different but similar in concept:

<script type="text/javascript">
function myFunct() {
    var url = '<%= Url.Action ("AjaxAction", "Home") %>';
    $.get(url, function(data) {
         // do stuff here
    });
}
</script>

And my HTML:

<a href="javascript:myFunct()">click here</a>

1 Comment

I'm going to try it again. I'm not sure what I'm doing wrong but maybe if I start over again I'll see what I missed.
0

When Web is the default page Your code ($.post("Home/AjaxAction");) is correct

Otherwise you have to specify(($.post("AjaxAction");) actionmethod Only

-Muthukumar

Comments

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.