简体   繁体   English

使用字体真棒心形图标切换(MVC)将产品添加到收藏夹

[英]Add product to favorite with font awesome heart icon toggle (MVC)

I need an ActionResult for adding my products by id to users fav.我需要一个ActionResult来按 id 添加我的产品给用户最喜欢的。 page.页。 I used jquery to toggle between two icons.我使用 jquery 在两个图标之间切换。 But I don't know how to pass product id to controller.但我不知道如何将产品 ID 传递给控制器​​。 Here are my codes:这是我的代码:

This is my view that has the product id:这是我的具有产品 ID 的视图:

<a href="#" id="addWish" class="like" product="@item.id">
  <i class="fa fa-heart-o" style="color:red;" id="wishlist"></i>
</a>

Here is my jquery script:这是我的 jquery 脚本:

<script>
  function addFav() {
    var productId = $(this).attr("product");
    $.ajax({
      url: "~/home/AddToFav",
      data: { "id": productId },
      success: function() {
        $('i#wishlist')
          .addClass('active')
          .attr('class', 'fa fa-heart-o')
          .unbind('click')
          .bind('click', removeFav);
      }
    });
  }
  function removeFav() {
    var productId = $(this).attr("product");
    $.ajax({
      url: "~/home/RemoveFromFav",
      data: { "id": productId },
      success: function() {
        $('i#wishlist')
          .removeClass('active')
          .attr('class', 'fa fa-heart')
          .unbind('click')
          .bind('click', addFav);
      }
    });
  }
  //this will make the link listen to function
  // addFav (you might know this already)
  $('a#wishlist').bind('click', addFav);
</script>

UPDATE更新

This is the generated html that i want to manage:这是我要管理的生成的 html:

<a href="#" class="addWish like" data-product="13">
  <i class="fa fa-heart-o" style="color:red;" id="wishlist"></i>
</a>
<a href="#" class="addWish like" data-product="12">
  <i class="fa fa-heart-o" style="color:red;" id="wishlist"></i>
</a>
<a href="#" class="addWish like" data-product="11">
  <i class="fa fa-heart-o" style="color:red;" id="wishlist"></i>
</a>

First of all, I don't think you need two functions for adding and removing.. Instead one function should decide whether to add or remove.首先,我不认为你需要两个函数来添加和删除。相反,一个函数应该决定是添加还是删除。 Eg if it is already in the users fav list, remove it or else add it..例如,如果它已经在用户收藏列表中,请将其删除或添加。

Secondly, you will also need some kind of a list_id or a user_id so that the method knows which user you are trying to update其次,您还需要某种 list_id 或 user_id 以便该方法知道您要更新哪个用户

Thirdly, for your a tag: append data- to the product as below.第三,对于您的标签:将数据附加到产品,如下所示。 Also, I believe you have more than one tags.另外,我相信你有不止一个标签。 So instead of an id use a class name to identify the clicked one..因此,不要使用 id 使用类名来标识单击的对象。

<a href="#" class="addWish like" data-product="@item.id">
  <i class="fa fa-heart-o" style="color:red;" id="wishlist"></i>
</a>

then wrap your function inside the click event of the a tag.然后将您的函数包装在 a 标签的 click 事件中。

$('.addWish').click(function(e) {
  e.preventDefault();
  var el = $(this),
    icon = el.find('i'), // <-- new
    productId = el.data("product"),
    some_user_id = 3; // get user id.. 
  $.ajax({
    url: '@Url.Action("toggleFavorite", "User")',
    data: {
      "id": productId,
      "user_id": some_user_id
    },
    done: function(result) {
      // our methods return true or false
      if (result) {
        alert('added to fav');
        icon.removeClass().addClass('fa fa-heart-o'); // <-- new
      } else {
        alert('removed from fav');
        icon.removeClass().addClass('fa fa-heart'); // <-- new
      }
    }
  });
});

Then in your controller;然后在您的控制器中;

public JsonResult toggleFavorite(int id, int user_id)
{
    // your db logic to check if the id is already in favourites
    // for this specific user.
    // then return true or false (or whatever) depending on your
    // requirements for this specific scenario assume we return
    // true: if we added
    // false: if we removed
    var result = // your db outcome
    return Json(result, JsonRequestBehavior.AllowGet);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM