dal层收藏的方法
public class MyFavoriteDAL
{
public int Favorite(string userid,string pnos)
{
string[] arr = pnos.Trim(',').Split(',');
foreach(string str in arr)
{
string sql = $"insert into MyFavorite(userid,pno) values('{userid}','{str}')";
DbAccess.DBHelper.ExecuteNonQuery(sql);//后期需要用事务
}
return 1;
}
}
后端代码
[HttpPost]
public int Favorite(string pnos)
{
string userid= HttpContext.Session["userid"].ToString();
return new MyFavoriteBLL().Favorite(userid, pnos);
}
ajax收藏
function MyFavorite() {
var arr = document.getElementsByName("xselect");
var str = "";
for (var i = 0; i < arr.length; i++) {
if (arr[i].checked)
str += arr[i].id + ",";
}
//alert(str);
$.ajax({
url: '/Home/Favorite',
type: 'post',
dataType: 'json',
data: { pnos: str },
success: function (data) {
if (data > 0)
alert("收藏成功!");
}
})
}