首页 > 代码库 > 【Asp.Net-- 杂七杂八】的代码
【Asp.Net-- 杂七杂八】的代码
Request.Url.PathAndQuery
public RedirectResult AddToCart(Cart cart, int productId, string returnUrl) { Product product = this.productRepository.Products .FirstOrDefault(p => p.ProductID == productId); if (null != productId) { cart.AddItem(product, 1); } returnUrl = this.Request["returnUrl"]; //return RedirectToAction("Index", new { returnUrl }); return Redirect(returnUrl); }
@model Easy5.ASPMVC.SportsStore.Domain.Entities.Product<div class="item"> <h3> @Model.Name </h3> @Model.Description @using(Html.BeginForm("AddToCart", "Cart")) { /* 1.创建一个隐藏的字段ProductID */ @Html.HiddenFor(x=>x.ProductID) /* 2.实参:"returnUrl" 必须与对应的参数的参数名名一样 */ @Html.Hidden("returnUrl", Request.Url.PathAndQuery)/*返回本页面的请求地址*/ <input type="submit" value=http://www.mamicode.com/"+ 加入购物车"/> } <h4> @Model.Price.ToString("c") </h4></div>
$.ajax
@Url.Action
<td style="text-align: center;"> <img src=http://www.mamicode.com/"@Url.Content("~/Images/Update.png")" alt="更新" title="更新" class="UpdateShoppingCartItemButton" id="@string.Format("update_{0}", item.ID)" /> </td> <td style="text-align: center;"> <img src=http://www.mamicode.com/"@Url.Content("~/Images/Delete.png")" alt="删除" title="删除" class="DeleteShoppingCartItemButton" id="@string.Format("delete_{0}", item.ID)" /> </td>@section scripts{ <script type="text/javascript"> function IsNumeric(input) { return (input - 0) == input && input.length > 0; } $(function () { $(".UpdateShoppingCartItemButton").click(function () { var buttonID = $(this).attr(‘id‘); var qid = buttonID.replace(‘update_‘, ‘quantity_‘); var itemID = buttonID.replace(‘update_‘, ‘‘); var quantity = $(‘#‘ + qid).val(); var postUrl = ‘@Url.Action("UpdateShoppingCartItem", "Home")‘; var redirectUrl = ‘@Url.Action("ShoppingCart", "Home")‘; if (!IsNumeric(quantity)) { alert(‘输入的数量值必须是数值。‘); return; } var intQuantity = parseInt(quantity); if (intQuantity <= 0) { alert(‘输入的数量值必须是大于或等于1的数值。‘); window.location.href = redirectUrl; return; } $.ajax({ type: "POST", url: postUrl, data: { shoppingCartItemID: itemID, quantity: intQuantity }, success: function (msg) { window.location.href = redirectUrl; } }); }); $(‘.DeleteShoppingCartItemButton‘).click(function () { if (confirm(‘是否确定要删除所选商品?‘)) { var buttonID = $(this).attr(‘id‘); var itemID = buttonID.replace(‘delete_‘, ‘‘); var postUrl = ‘@Url.Action("DeleteShoppingCartItem", "Home")‘; var redirectUrl = ‘@Url.Action("ShoppingCart", "Home")‘; $.ajax({ type: "POST", url: postUrl, data: { shoppingCartItemID: itemID }, success: function (msg) { window.location.href = redirectUrl; } }); } }); }); </script>}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。