Susen Maharjan
Susen Maharjan

Reputation: 537

How to call a Controller from button using jQuery

I have a button:

<button class="btn btn-info" id="details" value="@scr.Id">Details</button>

and I want to redirect it to my controller

public ActionResult Details(int par)
{
    var screening = _context.Screenings.Single(s => s.Id == par);
    return View(screening);
}

How can I achieve this using jQuery?

Upvotes: 0

Views: 2196

Answers (2)

Usman
Usman

Reputation: 4703

if you want to use JQuery to send id of the button you can try this

 $(document).ready(function() {
        $("#details").on('click', function (e) {
                e.preventDefault();
                window.location.href = '@Url.Action("Details", "ControllerName")?par=' + $(this).val();

            }
        );
    });

Upvotes: 0

Karthik Ganesan
Karthik Ganesan

Reputation: 4222

change you button to something like this

 @Html.ActionLink("Details", "Details", "Your Controller Name", new {@class = "btn btn-info"})

and it should work fine no need for JQuery

if you need jQuery

use ajax call

$("#details").click(function() {
 $.ajax({url: "/ControllerName/Details/YourIDValue", success: function(result){

    }});
})

Upvotes: 1

Related Questions