Nate Pet
Nate Pet

Reputation: 46222

MVC Call Controller/Method from Jquery

I have the following in my MVC View:

    <a href='@Url.Action("EmailPerson", "SupportNeeded")'> Email </a>

Below, SupportNeeded is the Controller and EmailPerson in the method

What is the equivalent if I do it in Jquery. Obviously I do not need the Email Lable. I just want it to automatically go to EmailPerson Method in SupportNeeded controller.

Note that I have EmailPerson and EmailPerson with HttpPost in my controller.

I tried in my Jquery

    '@Url.Action("EmailPerson", "SupportNeeded")'

but that didn't do anything for me. I also tried the .post :

    var url = '@Url.Action("EmailPerson", "SupportNeeded")';
    $.post(url), function (data) { });

but it went to the Method that has [HttpPost]. I need it to go to the method that does not have [HttpPost]. I then tried .get but did not do anything for me.

Upvotes: 0

Views: 2553

Answers (2)

Lester
Lester

Reputation: 4403

It's unclear what exactly you need. If you want to use $.get which will perform an ajax request, this works:

$.get("@Url.Action("EmailPerson", "SupportNeeded")");

If you're trying to redirect to that action you can do:

window.location.href = "@Url.Action("EmailPerson", "SupportNeeded")";

Upvotes: 0

Rick Burns
Rick Burns

Reputation: 1546

You should be able to accomplish this by using regular JavaScript.

window.location = '@Url.Action("EmailPerson", "SupportNeeded")';

Upvotes: 1

Related Questions