There are a few similar questions to this already, but they do not cover using values from the model to send as arguments to the controller.
Make DIV containing AJAX ActionLink clickable
Consider the following code in a Razor view:
@foreach (var item in Model)
{
<div class="itemDisplay">
<img src="~/Images/@item.DisplayImage" />
@Ajax.ActionLink($"Item {item.Id}", "_ItemDisplay", new { id = item.Id }, new AjaxOptions { UpdateTargetId = "itemDisplay", LoadingElementId = "ajax-loader", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }, null)
</div>
}
What I am trying to achieve is to apply that ActionLink
to the entire DIV
.
The problem when applying any of the JavaScript solutions is that I run into a problem of being unable to use JavaScript variables within Razor code (since one is client-side and the other server-side).
e.g:
function updateItemDisplay(itemId) {
$('#itemDisplay')
.click(function () {
$.ajax({
url: '@Url.Action("_ItemDisplay", new { id = *cannot use a JS variable here!* })',
type: "GET",
success: function (result) {
$('#itemDisplay').html(result);
}
});
});
};
So my question is, using ASP.Net MVC
, how can I make an AJAX call from a DIV tag and pass the relevant ID to the controller?