• 【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
  • 相关阅读:
    第123天:移动web开发中的常见问题
    第122天:移动端开发常见事件和流式布局
    第121天:移动端开发基本知识
    第120天:移动端-Bootstrap基本使用方法
    第119天:移动端:CSS像素、屏幕像素和视口的关系
    加入收藏 设为首页代码收藏本页的代码和收藏本站的代码设为首页代码
    JQuery和UpdatePannel的问题
    JS中apply与call的用法
    Sumlime text3 安装包、汉化包、注册码
    使用WITH AS提高性能简化嵌套SQL
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/3795530.html
Copyright © 2020-2023  润新知