ASP.NET MVC2.0实现数据的增、删、改、查(续)

3.试图。在views目录下创建product目录,添加视图。视图名称和conreol中的方法名(action名)一一对应.

在index视图中添加以下代码(这些代码都是对应界面上实现增删改查的。里面使用AJAX+JSON方式传送数据到Action):

 $(function () {
            //查看详细
            $(".ShowDetail").click(function () {
                //                debugger;
                var currentTr = $(this).parent().parent(); //当前行
                var currentTd = $("#tdID", currentTr).text(); //当前行所对应的productid
                var url = '<%=Url.Action("Detail") %>';
                url = url + "/" + currentTd;
                //                alert(currentTd);
                $.ajax({
                    url: url,
                    type: "POST",
                    dataType: "json",
                    success: ShowDetailResult
                })
            });
            function ShowDetailResult(result) {
                $("#txtProductID").val(result.ProductID);
                $("#txtProductName").val(result.ProductName);
                $("#txtProductPrice").val(result.ProductPrice);
                $("#txtProductDate").val(result.ProductDateStr);
                easyDialog.open({
                    container: 'divProductDetail'
                });
            }
            $("#aClose").click(function () {
                easyDialog.close();
            });

            //删除
            $(".del").click(function () {
                var url = '<%=Url.Action("Delete") %>';
                var currentTr = $(this).parent().parent(); //当前行
                var currentTd = $("#tdID", currentTr).text(); //当前行所对应的productid
                url = url + "/" + currentTd;
                //                alert(currentTd);
                $.ajax({
                    url: url,
                    type: "GET",
                    dataType: "json",
                    success: function () {
                        currentTr.remove();
                    }
                })
            });

            //修改
            var ctr; //当前行
            $(".modify").click(function () {
                var currentTr = $(this).parent().parent(); //当前行
                ctr = currentTr; //提交后移除当前行
                //debugger;
                var currentTd = $("#tdID", currentTr).text(); //当前行所对应的productid
                var url = '<%=Url.Action("Detail") %>';
                url = url + "/" + currentTd;
                //                alert(currentTd);
                $.ajax({
                    url: url,
                    type: "POST",
                    dataType: "json",
                    success: ShowResult
                })
            });
            function ShowResult(result) {
                $("#txtMProductID").val(result.ProductID);
                $("#txtMProductName").val(result.ProductName);
                $("#txtMProductPrice").val(result.ProductPrice);
                $("#txtMProductDate").val(result.ProductDateStr);
                easyDialog.open({
                    container: 'divModify'
                });
            }
            $("#aaClose").click(function () {
                easyDialog.close();
            });

            //提交
            $("#btnModify").click(function () {
                easyDialog.close();
                var productID = $("#txtMProductID").val();
                var productName = $("#txtMProductName").val();
                var productPrice = $("#txtMProductPrice").val();
                var productDate = $("#txtMProductDate").val();
                var productDateStr = productDate;
                var Product = { "ProductID": productID, "ProductName": productName, "ProductPrice": parseFloat(productPrice), "ProductDate": productDate, "ProductDateStr": productDateStr };
                var url = '<%=Url.Action("Modify") %>';
                //debugger;
                $.ajax({
                    url: url,
                    type: 'POST',
                    dateType: 'json',
                    data: $.toJSON(Product),
                    contentType: 'application/json;charset=utf-8',
                    success: function () {
                        ctr.remove();
                        //debugger;
                        ShowAddResult(Product);
                    }
                });
            });


            //添加
            $("#btnAdd").click(function () {
                var productID = $("#txtNewProductID").val();
                var productName = $("#txtNewProductName").val();
                var productPrice = $("#txtNewProductPrice").val();
                var productDate = $("#txtNewProductDate").val();

                var Product = { "ProductID": parseInt(productID), "ProductName": productName, "ProductPrice": productPrice, "ProductDate": productDate };
                var url = '<%=Url.Action("AddProduct") %>';
                $.ajax({
                    url: url,
                    type: 'POST',
                    dateType: 'json',
                    data: $.toJSON(Product),
                    contentType: 'application/json;charset=utf-8',
                    success: ShowAddResult
                });
            });
            function ShowAddResult(result) {
                //debugger;
                var tr1 = '<tr id="' + 1 + result.ProductID + '">';
                var td1 = '<td align="left"><a href="#" class="ShowDetail">查看详细</a>&nbsp;&nbsp;<a href="#" id="del" class="del">删除</a>&nbsp;&nbsp;<a href="#" id="modify" class="modify">修改</a></td>';
                var td2 = '<td id="tdID">' + result.ProductID + '</td>';
                var td3 = '<td>' + result.ProductName + '</td>';
                var td4 = '<td>' + result.ProductPrice + '</td>';
                var td5 = '<td>' + result.ProductDateStr + '</td>';
                var tr2 = '</tr>';
                var tr = tr1 + td1 + td2 + td3 + td4 + td5 + tr2;
                $(tr).appendTo('#tbl');

                $(".del").unbind("click").bind("click", function () {
                    var url = '<%=Url.Action("Delete") %>';
                    var currentTr = $(this).parent().parent(); //当前行
                    var currentTd = $("#tdID", currentTr).text(); //当前行所对应的productid
                    url = url + "/" + currentTd;
                    //alert(currentTd);
                    $.ajax({
                        url: url,
                        type: "GET",
                        dataType: "json",
                        success: function () {
                            currentTr.remove();
                        }
                    })
                });

                $(".ShowDetail").unbind("click").bind("click", function () {
                    var currentTr = $(this).parent().parent(); //当前行
                    var currentTd = $("#tdID", currentTr).text(); //当前行所对应的productid
                    var url = '<%=Url.Action("Detail") %>';
                    url = url + "/" + currentTd;
                    //                    alert(currentTd);
                    $.ajax({
                        url: url,
                        type: "POST",
                        dataType: "json",
                        success: ShowDetailResult
                    })
                });

                $(".modify").unbind("click").bind("click", function () {
                    var currentTr = $(this).parent().parent(); //当前行
                    var currentTd = $("#tdID", currentTr).text(); //当前行所对应的productid
                    ctr = currentTr; //提交后移除当前行
                    var url = '<%=Url.Action("Detail") %>';
                    url = url + "/" + currentTd;
                    //                    alert(currentTd);
                    $.ajax({
                        url: url,
                        type: "POST",
                        dataType: "json",
                        success: ShowResult
                    })
                });
            }
        });

4.界面展示

    <br />
    <div>
    <table id="tabAdd">
    <tr>
    <td>ProductID</td>
    <td><input type="text" id="txtNewProductID" class="required"/></td>
    <td>ProductName:</td>
    <td><input type="text" id="txtNewProductName" class="required"/></td>
    </tr>
    <tr>
    <td>ProductPrice:</td>
    <td><input type="text" id="txtNewProductPrice" /></td>
    <td>ProductDate:</td>
    <td><input type="text" id="txtNewProductDate" /></td>
    <td><input type="button" value="添加" id="btnAdd"/></td>
    </tr>
    </table>
    </div>

    <div>
    <% List<TestJquery.Models.Product> _ProductList = ViewData["ProductList"] as List<TestJquery.Models.Product>; %>
        <%if (_ProductList != null)
          {
        %>
        <table id="tbl">
        <thead><tr><td style="width:200px;">操作</td>
        <td style="width:50px;">ProductID</td>
        <td style="width:50px;">ProductName</td>
        <td style="width:50px;">ProductPrice</td>
        <td style="width:150px;">ProductDate</td></tr></thead>
        <tbody >
            <%foreach (TestJquery.Models.Product tmp in _ProductList)
              { %>
                <tr id="<%:1+tmp.ProductID  %>">
                <td align="left">
               <a href="#" class="ShowDetail">查看详细</a>&nbsp;&nbsp;
               <a href="#" id="del" class="del">删除</a>&nbsp;&nbsp;
               <a href="#" id="modify" class="modify">修改</a>
                </td>
                <td id="tdID">
                <%:tmp.ProductID %>
                </td>
                <td>
                <%:tmp.ProductName %>
                </td>
                <td>
                <%:tmp.ProductPrice %>
                </td>
                <td>
                <%:tmp.ProductDate %>
                </td>
                </tr>
            <%} %> 
        <%} %>
    </tbody>
    </table>
    </div>

    <!--弹出详细层-->
    <div id="divProductDetail" class="hide_box" style="display:none;">
        <h4><a id="aClose" href="javascript:void(0)" title="关闭窗口">&times;</a>详细信息</h4>
        <table>
        <tr>
        <td>ProductID:</td>
        <td><input type="text" id="txtProductID" /></td>
        </tr>
        <tr>
        <td>ProductName:</td>
        <td><input type="text" id="txtProductName" /></td>
        </tr>
        <tr>
        <td>ProductPrice:</td>
        <td><input type="text" id="txtProductPrice" /></td>
        </tr>
        <tr>
        <td>ProductDate:</td>
        <td><input type="text" id="txtProductDate" /></td>
        </tr>
        </table>
    </div>

    <!--弹出修改层-->
    <div>
    <div id="divModify" class="hide_box" style="display:none;">
        <h4><a id="aaClose" href="javascript:void(0)" title="关闭窗口">&times;</a>修改信息</h4>
        <table>
        <tr>
        <td>ProductID:</td>
        <td><input type="text" id="txtMProductID" /></td>
        </tr>
        <tr>
        <td>ProductName:</td>
        <td><input type="text" id="txtMProductName" /></td>
        </tr>
        <tr>
        <td>ProductPrice:</td>
        <td><input type="text" id="txtMProductPrice" /></td>
        </tr>
        <tr>
        <td>ProductDate:</td>
        <td><input type="text" id="txtMProductDate" /></td>
        </tr>
        <tr>
        <td>&nbsp;</td>
        <td align="center"><input id="btnModify" type="button" value="提交" /></td>
        </tr>
        </table>
    </div>
    </div>

5.效果图:  源码下载地址:http://download.csdn.net/source/3559195

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值