Search code examples
jqueryajaxasp.net-coreview

asp.net core <a asp-controller="" asp-action=""/> run from jquery


Net Core Mvc 6

I have a Menu like this

<li class="active nav-item h5"><a class="nav-link link-light aEmployer" asp-controller="Employer" asp-action="Index">Employer Setup</a></li>

That returns a View

I need to call this from Ajax.

I sow this and what it says to do

$.ajax({
    // edit to add steve's suggestion.
    //url: "/ControllerName/ActionName",
    url: '<%= Url.Action("ActionName", "ControllerName") %>',
    success: function(data) {
         alert(data);
    }
});

But in this case, it returns a PartialView.

What I need is to return a View.. the same View that is returned when the user click the Option Menu.

What I need is something like ('aa').click() but it does not work in this case...

It is a way to call it?

Thanks


Solution

  • Set the id attribute of your anchor element:

    <a id="employer-setup-link" class="nav-link link-light aEmployer" asp-controller="Employer" asp-action="Index">Employer Setup</a>
    

    And then you can do:

    document.getElementById('employer-setup-link').click();
    

    You can also try:

    window.location.href = document.getElementById('employer-setup-link').getAttribute('href');