在不重定向的情况下执行操作(添加到愿望清单)
Execute action without redirecting(Add to wishlist)
所以,我有一些产品卡片要用 foreach 循环显示。当我单击 link 时,我想将该产品添加到我的愿望清单 (Session["wishList"])。这会调用一个函数并将我重定向到 /product/wishlist/01,其中 01 是产品 ID。
我的临时修复是return Content("<script type='text/javascript'>window.history.back();</script>");
,但不重新加载会更好。
我的代码:
<code>
<a href="@Url.Action("AddToWishList", "Product", new {id = item.Id})"><i class="far fa-heart"></i></a>
</code>
控制器:
<code>
public ActionResult AddToWishList(int id)
{
if (Session["email"] != null && Session["role"] != null && (WebApplication1.Models.RoleType)Session["role"] == Models.RoleType.User)
{
var wishList = (List<int>)Session["wishList"];
if (wishList.Contains(id))
{
wishList.Remove(id);
}
else
{
wishList.Add(id);
}
Session["wishList"] = wishList;
}
return Content("<script type='text/javascript'>window.history.back();</script>");
}</code>
您可以在您的情况下使用 Ajax
,它用于更新页面的一部分而无需重新加载整个页面。在您的情况下,您可以执行以下操作:
定义一个函数,它将接受所选元素的 id
:
<code>
<a href="#" onClick='addToWishList('@item.Id')'><i class="far fa-heart"></i></a>
</code>
然后你需要定义一个函数,它将接受这个 id
并通过 Ajax
:
将它发送到你的 Controller
<script>
function addToWishList(id)
{
var json = {
id : id
};
$.ajax({
type: 'POST',
url: "@Url.Action("AddToWishList", "Product")",
dataType : "json",
data: {"json": JSON.stringify(json)},
success: function(data) {
if(data.status=="true")
{
alert(data.msg);
var urlToRedirect= '@Url.Action("Index","Home")';
window.location.href = urlToRedirect; //Redirect here
}
},
error: function(data) {
alert('Some error');
window.location.reload();
}
});
}
</script>
然后最后在您的 Controller
中,您可以处理此 id
然后 return 返回结果:
using System.Web.Script.Serialization;
[HttpPost]
public ActionResult AddToWishList(string json)
{
var serializer = new JavaScriptSerializer();
dynamic jsondata = serializer.Deserialize(json, typeof(object));
//Get your variables here from AJAX call
var id = Convert.ToInt32(jsondata["id"]);
if (Session["email"] != null && Session["role"] != null && (WebApplication1.Models.RoleType)Session["role"] == Models.RoleType.User)
{
var wishList = (List<int>)Session["wishList"];
if (wishList.Contains(id))
{
wishList.Remove(id);
}
else
{
wishList.Add(id);
}
Session["wishList"] = wishList;
}
return Json(new {status="true", msg= "Successfully processed"});
}
您可以根据需要定制方法。
在您的页面中实施回调可行:
所以,我有一些产品卡片要用 foreach 循环显示。当我单击 link 时,我想将该产品添加到我的愿望清单 (Session["wishList"])。这会调用一个函数并将我重定向到 /product/wishlist/01,其中 01 是产品 ID。
我的临时修复是return Content("<script type='text/javascript'>window.history.back();</script>");
,但不重新加载会更好。
我的代码:
<code>
<a href="@Url.Action("AddToWishList", "Product", new {id = item.Id})"><i class="far fa-heart"></i></a>
</code>
控制器:
<code>
public ActionResult AddToWishList(int id)
{
if (Session["email"] != null && Session["role"] != null && (WebApplication1.Models.RoleType)Session["role"] == Models.RoleType.User)
{
var wishList = (List<int>)Session["wishList"];
if (wishList.Contains(id))
{
wishList.Remove(id);
}
else
{
wishList.Add(id);
}
Session["wishList"] = wishList;
}
return Content("<script type='text/javascript'>window.history.back();</script>");
}</code>
您可以在您的情况下使用 Ajax
,它用于更新页面的一部分而无需重新加载整个页面。在您的情况下,您可以执行以下操作:
定义一个函数,它将接受所选元素的 id
:
<code>
<a href="#" onClick='addToWishList('@item.Id')'><i class="far fa-heart"></i></a>
</code>
然后你需要定义一个函数,它将接受这个 id
并通过 Ajax
:
Controller
<script>
function addToWishList(id)
{
var json = {
id : id
};
$.ajax({
type: 'POST',
url: "@Url.Action("AddToWishList", "Product")",
dataType : "json",
data: {"json": JSON.stringify(json)},
success: function(data) {
if(data.status=="true")
{
alert(data.msg);
var urlToRedirect= '@Url.Action("Index","Home")';
window.location.href = urlToRedirect; //Redirect here
}
},
error: function(data) {
alert('Some error');
window.location.reload();
}
});
}
</script>
然后最后在您的 Controller
中,您可以处理此 id
然后 return 返回结果:
using System.Web.Script.Serialization;
[HttpPost]
public ActionResult AddToWishList(string json)
{
var serializer = new JavaScriptSerializer();
dynamic jsondata = serializer.Deserialize(json, typeof(object));
//Get your variables here from AJAX call
var id = Convert.ToInt32(jsondata["id"]);
if (Session["email"] != null && Session["role"] != null && (WebApplication1.Models.RoleType)Session["role"] == Models.RoleType.User)
{
var wishList = (List<int>)Session["wishList"];
if (wishList.Contains(id))
{
wishList.Remove(id);
}
else
{
wishList.Add(id);
}
Session["wishList"] = wishList;
}
return Json(new {status="true", msg= "Successfully processed"});
}
您可以根据需要定制方法。
在您的页面中实施回调可行: