【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);
        }
View Code
@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="+ 加入购物车"/>
    }

    <h4>
        @Model.Price.ToString("c")
    </h4>
</div>

        $.ajax       

@Url.Action
                        <td style="text-align: center;">
                            <img src="@Url.Content("~/Images/Update.png")" alt="更新" title="更新" class="UpdateShoppingCartItemButton" id="@string.Format("update_{0}", item.ID)" />
                        </td>
                        <td style="text-align: center;">
                            <img src="@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>
}
View Code
原文地址:https://www.cnblogs.com/easy5weikai/p/3795530.html