ITWorker ITWorker - 2 months ago 16x
HTML Question

How to perform Html.Action on javascript event?

I have a div within a cshtml file that shows the view from another action, as follows:

<div id="tabs-1ua">
@Html.Action("Index", "Admin")

This loads fine on initial page load. In a separate js file, I have an event catcher for the div as follows:

$("a[href='#tabs-1ua']").click(function (event, ui) {

$("#tabs-1ua").val("@Html.Action('Index', 'Admin')");


As can be seen above, the val parameter is the same Razor code that appears in the original div. However, this doesn't get me the desired outcome of the action being performed each time that div event is activated. What is the correct way to activate the razor code each time the event occurs? I made sure the event is captured by testing with alerts, so that's not the issue.

Thank you.


You can try to load the desired content asynchronously using ajax, doing something like this:

$("a[href='#tabs-1ua']").click(function (event, ui) {

       url: "/Admin/Index",
       data: {}
   }).done(function (htmlResponse) {

This loads the result of the action inside the div with id=tabs-1ua.

Maybe this is a simpler approach, let me know if i can be of further help