zoukankan      html  css  js  c++  java
  • MVC增删改查整套(ajax,两个项目跨域)


    ///API
    namespace WebApplication1.Controllers
    {
         [EnableCors("*", "*", "*")]
        public class BookApiController : ApiController
        {
            
            day19_BookManagerEntities book = new day19_BookManagerEntities();
            
            // GET: api/BookApi
            public IEnumerable<Book> Get()
            {
                return book.Book;
            }
            [HttpGet]
            public List<Book> Get(int id)
            {
                return book.Book.Where(T => T.Book_ID==id).ToList();
            }
            // GET: api/BookApi/5
            public IEnumerable<Book> GetName(string id)
            {
                return book.Book.Where(T => T.Book_Name.Contains(id)).ToList();
            }

            // POST: api/BookApi
            public void Post([FromBody]Book value)
            {
                book.Book.Add(value);
                book.SaveChanges();
            }

            // PUT: api/BookApi/5
            public void Put(int id, [FromBody]Book value)
            {
                var b = book.Book.Where(T => T.Book_ID == id).FirstOrDefault();
                if (b != null)
                {
                    b.Book_ID = value.Book_ID;
                    b.Book_Name = value.Book_Name;
                    b.Book_Num = value.Book_Num;
                    b.Book_Price = value.Book_Price;
                }
                book.SaveChanges();
            }

            // DELETE: api/BookApi/5
            public HttpResponseMessage Delete(int id)
            {
                var b = book.Book.Where(T => T.Book_ID == id).FirstOrDefault();
                try
                {
                    if (b != null)
                    {
                        book.Book.Remove(b);
                        book.SaveChanges();
                        return new HttpResponseMessage() { StatusCode = HttpStatusCode.OK };
                    }
                    else
                    {
                        return new HttpResponseMessage() { StatusCode = HttpStatusCode.NoContent };
                    }
                }
                catch (Exception)
                {
                    return new HttpResponseMessage() { StatusCode = HttpStatusCode.InternalServerError };
                }
            }
        }
    }



        

    ///路由WebApiconfing(切记改路由,在Api里做修改)
         public static void Register(HttpConfiguration config)
            {
                config.EnableCors();
                // Web API 配置和服务

                // Web API 路由
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
                config.Routes.MapHttpRoute(
                 name: "ActionApi",
                 routeTemplate: "Actionapi/{controller}/{action}/{id}",
                 defaults: new { id = RouteParameter.Optional }
             );
            }




    ////显示页面
    <body>
        <div>
            <a href="/BookAction/Add">添加</a>
            <input type="text" id="txtname" placeholder="请输入书名"  />
            <a href="#" onclick="Select()">查询</a>
            <table>
                <tr>
                    <td>书名</td>
                    <td>数量</td>
                    <td>价格</td>
                    <td>删除</td>
                </tr>
                <tbody id="tbody1">

                </tbody>
            </table>

        </div>

        <script>
            $.ready(Show());
            function Show() {
                $.ajax({
                    url: "http://localhost:57173/api/BookApi",
                    type: "Get",
                    success: function (data) {
                        var str = "";
                        for (var i = 0; i < data.length; i++) {
                            str += "<tr>";
                            str += "<td>" + data[i].Book_Name + "</td>"
                            str += "<td>" + data[i].Book_Num + "</td>"
                            str += "<td>" + data[i].Book_Price + "</td>"
                            str += "<td><input  type="button" onclick='Del(" + data[i].Book_ID + ")' value="删除" /></td>"
                            str += "<td><input  type="button" onclick='Upt(" + data[i].Book_ID + ")' value="修改" /></td>"
                            str += "</tr>";
                        }
                        $("#tbody1").html(str);
                    }
                })
            }

            function Del(id) {
                $.ajax({
                    url: "http://localhost:57173/api/BookApi/"+id,
                    type: "Delete",
                    success: function () {
                        alert("删除成功");
                        Show();
                    }
                })
            }

            function Upt(id) {
                location.href = "/BookAction/Upt?id=" + id;
            }

            function Select() {
                var name = $("#txtname").val();
                if (name!="") {
                    $.ajax({
                        url: "http://localhost:57173/Actionapi/BookApi/GetName/" + name,
                        type: "Get",
                        success: function (data) {
                            var str = "";
                            for (var i = 0; i < data.length; i++) {
                                str += "<tr>";
                                str += "<td>" + data[i].Book_Name + "</td>"
                                str += "<td>" + data[i].Book_Num + "</td>"
                                str += "<td>" + data[i].Book_Price + "</td>"
                                str += "<td><input  type="button" onclick='Del(" + data[i].Book_ID + ")' value="删除" /></td>"
                                str += "<td><input  type="button" onclick='Upt(" + data[i].Book_ID + ")' value="修改" /></td>"
                                str += "</tr>";
                            }
                            $("#tbody1").html(str);
                        }
                    })
                }
                else {
                    Show();
                }
                
            }

        </script>
    </body>
    </html>


    ///Add页面
    <body>
        <div>
            <table>
                <tr>
                    <td>书名</td>
                    <td>
                        <input type="text" name="name" id="txtname" />
                    </td>
                </tr>
                <tr>
                    <td>数量</td>
                    <td>
                        <input type="text" name="name" id="txtnum" />
                    </td>
                </tr>
                <tr>
                    <td>价格</td>
                    <td>
                        <input type="text" name="name" id="txtprice"/>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button"  name="name" value="提交"onclick="Add()" />
                    </td>
                </tr>
            </table>
        </div>
        <script>
            function Add() {
                $.ajax({
                        url: "http://localhost:57173/api/BookApi",
                        type: "Post",
                        dataType: "json",
                        data:{Book_Name:$("#txtname").val(),Book_Num:$("#txtnum").val(),Book_Price:$("#txtprice").val()},
                        success: function () {
                            alert("提交成功");
                            location.href = "/BookAction/Index";
                        }
                })
            }
        </script>
    </body>
    </html>

    ////修改页面
    <body>
        <div>
            <table>
                <tr>
                    <td>书名</td>
                    <td>
                        <input type="text" name="name" id="txtname" />
                    </td>
                </tr>
                <tr>
                    <td>数量</td>
                    <td>
                        <input type="text" name="name" id="txtnum" />
                    </td>
                </tr>
                <tr>
                    <td>价格</td>
                    <td>
                        <input type="text" name="name" id="txtprice" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="name" id="txtid" />
                    </td>
                    <td>
                        <input type="button" name="name" value="提交" onclick="Upt()" />
                    </td>
                </tr>
            </table>
        </div>
        <script>
            //$.ready(Show()
               
            //    );
            $(function () {
                Show();
            })
            function Show() {
                var id = location.search.split('=')[1];
                alert(id);
                $.ajax({
                    url: "http://localhost:57173/Actionapi/BookApi/Get/"+id,
                    type: "Get",
                    success: function (data) {
                        for (var i = 0; i < data.length; i++) {
                            $("#txtid").val(data[i].Book_ID);
                            $("#txtname").val(data[i].Book_Name);
                            $("#txtnum").val(data[i].Book_Num);
                            $("#txtprice").val(data[i].Book_Price);
                        }
                    }
                })
            }

            function Upt() {
                var id = location.search.split("=")[1];
                alert(id);
                $.ajax({
                    url: "http://localhost:57173/api/BookApi/"+id,
                    type: "Put",
                    dataType: "json",
                    data: { Book_Name: $("#txtname").val(), Book_Num: $("#txtnum").val(), Book_Price: $("#txtprice").val(),Book_ID:id},
                    success: function () {
                        alert("提交成功");
                        location.href = "/BookAction/Index";
                    }
                })
            }
        </script>
    </body>
    </html>

  • 相关阅读:
    SIT/UAT测试
    Oracle密码过期设置和修改密码问题
    1、查询速度慢的原因很多,常见如下几种:
    dbs:apple-notes
    值不能为 null 或为空。参数名: linkText
    Visual Stadio 2015创建WebApplication应用和运行赏析
    HTTP 错误 500.19
    Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
    vs2015-Azure Mobile Service
    6.1.1 验证注解的使用
  • 原文地址:https://www.cnblogs.com/jpp666/p/7883088.html
Copyright © 2011-2022 走看看