1、切记,页面必需要引用 jquery.unobtrusive-ajax.min.js。
前端
@Ajax.ActionLink("***","AddItem","shop",new { t=new Random().NextDouble() },new AjaxOptions { HttpMethod = "Get",OnBegin = "OnBegin",OnSuccess="OnSuccess",OnComplete = "OnComplete" })
脚本
function OnBegin() { //弹出遮罩层,防止用户在服务器响应期间的其他误点击 } function OnSuccess(data) { alert(data);//接收action传来的返回值 } function OnComplete() { //关闭遮罩层 }
[HttpGet]//对应ActionLick中的http方法 public string AddItem(int? productId) { int ProductID=productId ?? 0; if(!Common.CheckProduct(ProductID)) return "nodata"; //TUser UserModel=System.Web.HttpContext.Current.Session["UserModel"] as TUser; TProduct product = TProductConnection.GetById(ProductID); TShoppingCart item = new TShoppingCart(); item.BuyNumber = 1; //item.UserID = UserModel.UserID; item.UserID = 1; item.IsDel = 0; item.ProductID = ProductID; item.ProductName = product.ProductName; item.Price = product.Price; item.AddTime = DateTime.Now; TShoppingCartConnection.Add(item); try { UnitOfWork.Commit(); return "success"; } catch { return "fail"; } }原文链接:https://www.f2er.com/ajax/161661.html