By looking at the posted code.
1. the id attribute for each element should be unique, but it was hard coded as "ID" in the loop. I think that why the second loop didn't create a new hidden control because ID already exists.
@Html.HiddenFor(modelItem => item.ID, new { id = "ID" })
2. The easiest fix is to pass in the id to myfunction, see below
<input type="button" value="Assign" onclick="myfunction(@item.ID)" />
3. then update the myfunction code to use the id variable
function myfunction(id)
{
alert(id);
}
4. I think same issue with the
CollectionName
, you might need to update the code to pass that to the myfunction as well
<input type="button" value="Assign" onclick="myfunction(@item.ID, @item.CollectionName)" />
function myfunction(id, collectionName)
{
alert(collectionName);
alert(id);
}
Note: I did not verify the syntax. but hopefully you'll get the idea.
javascript - how to pass a razor value to a jquery function, in an mvc view - Stack Overflow[
^]