zoukankan      html  css  js  c++  java
  • MVC增删改查(API一个项目下拉框查询)


    //////页面跳转
    <a href="/Home/Client_Delete/@item.BookID">删除</a>
    <a href="/Home/Client_Put/@item.BookID">修改</a>

    ///////WebApiConfig里修改路由配置
    public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                
                // Web API 配置和服务
                // 将 Web API 配置为仅使用不记名令牌身份验证。
                config.SuppressDefaultHostAuthentication();
                config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

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

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

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







    //////Api页面方法
        day20_BookEntities db = new day20_BookEntities();
            // GET: api/BookApi
            public IEnumerable<OrderInfo> Get()
            {
                var data = from s in db.Book
                           join ss in db.TypeInfo on s.BookTid equals ss.Tid
                           select new OrderInfo()
                           {
                               BookID = s.BookID,
                               BookName = s.BookName,
                               BookTid = s.BookTid,
                               BookNum = s.BookNum,
                               BookPrice = s.BookPrice,
                               Tname = ss.Tname,
                           };
                return data.ToList();
            }
            [HttpGet]
            public List<OrderInfo> Select(int id)
            {
                var data = from s in db.Book
                           join ss in db.TypeInfo on s.BookTid equals ss.Tid
                           where s.BookTid == id
                           select new OrderInfo()
                           {
                               BookID = s.BookID,
                               BookName = s.BookName,
                               BookTid = s.BookTid,
                               BookNum = s.BookNum,
                               BookPrice = s.BookPrice,
                               Tname = ss.Tname,
                           };
                return data.ToList();
            }
            // POST: api/BookApi
            public void Post([FromBody]Book value)
            {
                db.Book.Add(value);
                db.SaveChanges();
            }

            // PUT: api/BookApi/5
            public void Put(int id, [FromBody]OrderInfo value)
            {
                var b = db.Book.Where(T => T.BookID == value.BookID).FirstOrDefault();
                if (b != null)
                {
                    b.BookID = value.BookID;
                    b.BookName = value.BookName;
                    b.BookTid = value.BookTid;
                    b.BookNum = value.BookNum;
                    b.BookPrice = value.BookPrice;
                }
                db.SaveChanges();
            }

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




    public ActionResult Client_Get()
            {
                //下拉列表
                day20_BookEntities db = new day20_BookEntities();
                var selectlist = db.TypeInfo;
                SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
                ViewBag.SelectList = list2;


                Uri url = new Uri("http://localhost:52444");
                HttpClient client = new HttpClient();
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage message = client.GetAsync("api/BookApi").Result;
                List<OrderInfo> list = new List<OrderInfo>();
                if (message.IsSuccessStatusCode)
                {
                    list = message.Content.ReadAsAsync<List<OrderInfo>>().Result;
                }
                client.Dispose();
                return View(list);
            }
            public ActionResult Client_Select(int id)
            {
                //下拉列表
                day20_BookEntities db = new day20_BookEntities();
                var selectlist = db.TypeInfo;
                SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
                ViewBag.SelectList = list2;

                 
                Uri url = new Uri("http://localhost:52444");
                HttpClient client = new HttpClient();
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage message = client.GetAsync("Actiontapi/BookApi/Select/" + id).Result;
                List<OrderInfo> list = new List<OrderInfo>();
                if (message.IsSuccessStatusCode)
                {
                    list = message.Content.ReadAsAsync<List<OrderInfo>>().Result;
                }
                client.Dispose();
                return View("Client_Get", list);
            }
            public ActionResult Client_Post()
            {
                //下拉列表
                day20_BookEntities db = new day20_BookEntities();
                var selectlist = db.TypeInfo;
                SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
                ViewBag.SelectList = list2;

                return View();
            }
            [HttpPost]
            public ActionResult Client_Post(Book o)
            {
                Uri url = new Uri("http://localhost:52444");
                HttpClient client = new HttpClient();
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpContent content = new StringContent("{BookName:'" + o.BookName + "',BookTid:'" + o.BookTid + "',BookNum:'" + o.BookNum + "',BookPrice:'" + o.BookPrice + "'}");
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage message = client.PostAsync("api/BookApi",content).Result;
                if (message.IsSuccessStatusCode)
                {
                    client.Dispose();
                    return Content("<script>alert('提交成功');location.href='/Home/Client_Get'</script>");
                }
                else
                {
                    return Content("<script>alert('提交成功')</script>");
                }
            }

            public ActionResult Client_Delete(int id)
            {
                Uri url = new Uri("http://localhost:52444");
                HttpClient client = new HttpClient();
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage message = client.DeleteAsync("api/BookApi/"+id).Result;
                if (message.IsSuccessStatusCode)
                {
                    client.Dispose();
                    return Content("<script>alert('删除成功');location.href='/Home/Client_Get'</script>");
                }
                else
                {
                    return Content("<script>alert('删除成功')</script>");
                }
            }

            public ActionResult Client_Put(int id)
            {
                //下拉列表
                day20_BookEntities db = new day20_BookEntities();
                var selectlist = db.TypeInfo;
                SelectList list2 = new SelectList(selectlist, "Tid", "Tname");
                ViewBag.SelectList = list2;


                var data = db.Book.Where(T => T.BookID == id).FirstOrDefault();
                var result = new OrderInfo()
                {
                    BookID = data.BookID,
                    BookName = data.BookName,
                    BookNum = data.BookNum,
                    BookPrice = data.BookPrice,
                    BookTid = data.BookTid,
                };

                return View(result);
            }
            [HttpPost]
            public ActionResult Client_Put(Book o)
            {
                Uri url = new Uri("http://localhost:52444");
                HttpClient client = new HttpClient();
                client.BaseAddress = url;
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                HttpContent content = new StringContent("{BookID:'" + o.BookID + "',BookName:'" + o.BookName + "',BookTid:'" + o.BookTid + "',BookNum:'" + o.BookNum + "',BookPrice:'" + o.BookPrice + "'}");
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
                HttpResponseMessage message = client.PutAsync("api/BookApi/"+o.BookID ,content).Result;
                if (message.IsSuccessStatusCode)
                {
                    client.Dispose();
                    return Content("<script>alert('提交成功');location.href='/Home/Client_Get'</script>");
                }
                else
                {
                    return Content("<script>alert('提交成功')</script>");
                }
            }

  • 相关阅读:
    VSCode中快捷键位设置优化
    错误检测(4) CRC
    linux 中的col命令
    使Chrome的字体渲染更清晰
    并发工具类(三)控制并发线程的数量 Semphore
    并发工具类(二)同步屏障CyclicBarrier
    并发工具类(一)等待多线程的CountDownLatch
    显式锁(四)Lock的等待通知机制Condition
    显式锁(三)读写锁ReadWriteLock
    显式锁(二)Lock接口与显示锁介绍
  • 原文地址:https://www.cnblogs.com/jpp666/p/7883084.html
Copyright © 2011-2022 走看看