[英]Sending data from view to controller through ajax in asp.net
i create a add to favorite button by that i send the value of id of the button from view to controller through javascript and ajax in asp.net core mvc.我通过将按钮的 id 值从视图发送到 controller 通过 asp.net 核心 mvc 中的 javascript 和 ajax 创建了一个添加到收藏夹按钮。 But value of id always 1 even i click on another favorite button.
但 id 的值始终为 1,即使我单击另一个最喜欢的按钮。 Kindly tell me where i do mistake.
请告诉我我在哪里做错了。 My view:
我的看法:
@foreach (var item in Model)
{
<div>
<input id="houseid" type="hidden" value="@item.id">
<a href="#" class="phone_number" id="favorite" onclick="favoritefunction()" >
</div>
}
<script>
function favoritefunction() {
var id = document.getElementById("houseid").value;
debugger
var xhttp = new XMLHttpRequest();
xhttp.open("Get", "/Home/ajaxRent?id="+id, true);
xhttp.send();
alert("favorite added successfully");
}
</script>
Id
should be unique,so each time the value of document.getElementById("houseid").value
will find the same value.You can try to pass @item.id to favoritefunction
.Here is a demo: Id
应该是唯一的,所以每次document.getElementById("houseid").value
的值都会找到相同的值。你可以尝试将 @item.id 传递给favoritefunction
。这是一个演示:
@foreach (var item in Model)
{
<div>
<input id="houseid" type="hidden" value="@item.id">
<a href="#" class="phone_number" id="favorite" onclick="favoritefunction(@item.id)" >a</a>
</div>
}
<script>
function favoritefunction(id) {
var xhttp = new XMLHttpRequest();
xhttp.open("Get", "/Home/ajaxRent?id="+id, true);
xhttp.send();
alert("favorite added successfully");
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.