In my view i have two divs with controller and action method name in data-url attribute. When the page gets loaded, i am looping through these div classes and getting the name of the constructor and action method to pass to ajax call. However now im having hard time using @Url.Action method to generate the url to make the ajax call. Does anyone know how i can pass javascript variable in @Url.Action. In my code, i am setting the data-url value in partialDivUrl variable. Now in my ajax call i want to be able to do @Url.Action(partialDivUrl) or something similar to this which creates the right url. I am trying to load partial view and please do not suggest to use @Html.Action, @Html.Partial or @Html.Render..etc..since the action method is async method.
Here is the html:
<div class="partialContents" data-url="/Work/LoadEmployeePartial"></div>
<div class="partialContents" data-url="/Work/LoadSupervisorPartial"></div>
<script type="text/javascript">
$(document).ready(function (e) {
$(".partialContents").each(function (index, item) {
var partialDivUrl = $(item).data("url");
$.ajax({
url: partialDivUrl, <----- want to use @Url.Action here
type: 'GET',
data: {employeeId: @Model.EmployeeId},
contentType: 'application/json',
success: function (data) {
if (data != null) {
$(item).html(data);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("Unexpected error.")
}
});
});
});
</script>
codevar partialDivUrl = 'domainName' +$(item).data("url") . Hope that makes sense.