So, i works in a project and i have a problem when i pass data from my view to my javascript,i have a foreach loop but always i get the last elements, this is my view :
@foreach (var al in Model.alerts)
{
<table border="0">
<tr>
<td rowspan=3><img src= '@Html.Raw(al.Owpicture)' class="img" alt="IMAGES"/></td>
<td><label class="lb">@Html.Raw(al.cinOw)</label></td>
</tr>
<tr>
<td><label class="lb">@Html.Raw(al.Owname)</label></td>
</tr>
<tr>
<td><label class="lb">@Html.Raw(al.OwpermitNumber)</label></td>
</tr>
</table>
<script>
//this is variable to pass
var carNum = '@Html.Raw(al.carNumber)';
</script>
<input type="submit" name="Report" value="Report" onclick="IsReport(carNum)" />
and this is my script code :
function IsReport(carNum)
{
var url = '/Home/IsReport/' + carNum;
$.ajax({
type: "Post",
url: url,
data: {},
datatype: 'json',
success: function (data) { alert(carNum); },
});
}
i tried everything but always i get the last carNum, so please if someone have any idea i will be very appreciate .
carNumwith the current value.<script>blocks in which you setcarNumare all executed at page rendering, and so only this variable takes the last value you set it to. So, by the time anyonclickhandler is run, it will always take this value. If you pass the value directly in the handler however, it will work.onclickhandlers.