<ul>
@foreach (var item in Model.Users.Where(s => s.DepartmentId == 3))
{
<li>
@Ajax.ActionLink(item.UserName, "Index", new { item.Id }, new AjaxOptions() { HttpMethod = "Get" });
</li>
}
</ul>
How can ı get item.Id if i onclick item.username with javascript ?
I thought give to attribute to "li onclick".but i dont know there is better solution here or not ?
You could use a standard link and data-* attribute:
<ul>
@foreach (var item in Model.Users.Where(s => s.DepartmentId == 3))
{
<li>
@Html.ActionLink(
item.UserName,
"Index",
new { id = item.Id },
new { @class = "myLink", data_id = item.Id }
)
</li>
}
</ul>
and then in a separate javascript file:
$(function() {
$('.myLink').click(function() {
// get the id:
var id = $(this).data('id');
alert(id);
// now you can send the AJAX request
$.get(this.href);
// cancel the default action of the link
return false;
});
});