• Api+Mvc增删查改


    ***********************************************************************************************************

    Api:

        public class StuController : ApiController
        {
        //实例化对象
            Day3Entities ent = new Day3Entities();

        //显示

            // GET api/stu
            public List<Student> Get()
            {
                return ent.Students.ToList();
            }

        //查询

             //GET api/stu/5
            public Student Get(string name)
            {
                var list = ent.Students.Where(T=>T.NAME.Contains(name)).FirstOrDefault();
                return list;
            }
            //public Student Get(int id)
            //{
            //    var list = ent.Students.Where(T => T.ID==id).FirstOrDefault();
            //    return list;


            //}
        
        //添加

            // POST api/stu
            public void Post([FromBody]Student stu)
            {
                var res = ent.Students.Add(stu);
                ent.SaveChanges();
            }

        //修改
         
            // PUT api/stu/5
            public void Put([FromBody]Student stu)
            {
                var res = ent.Students.Where(T => T.ID == stu.ID).FirstOrDefault();
                res.NAME = stu.NAME;
                res.Sex = stu.Sex;
                res.Age = stu.Age;

                ent.SaveChanges();
            }

        //删除

            // DELETE api/stu/5
            public void Delete(int id)
            {
                var res = ent.Students.Where(T => T.ID == id).FirstOrDefault();
                ent.Students.Remove(res);
                ent.SaveChanges();

            }
        }

    ***********************************************************************************************************

    //显示数据

    public class StuMvcController : Controller
        {
            //
            // GET: /StuMvc/

            //定义一个基地址
            public static readonly Uri address = new Uri("http://localhost:61136/");
            public ActionResult Index(int pageIndex=1)
            {
                //实例化泛型
                List<Student> list = new List<Student>();

                //获取api的方法
                Uri url = new Uri(address, "/api/stu");
                //引用命名空间
                using (HttpClient client = new HttpClient())
                {
                    var result = client.GetAsync(url).Result;
                    //判断返回值状态
                    if (result.IsSuccessStatusCode)
                    {
                        list = result.Content.ReadAsAsync<List<Student>>().Result;
                    }
                }
                return View(list.ToPagedList(pageIndex,3));
            }

            /// <summary>
            /// 添加显示页面
            /// </summary>
            /// <returns></returns>
            public ActionResult Add()
            {
                //自定义下拉列表
                List<SelectListItem> liAge = new List<SelectListItem>() {
                new SelectListItem(){Value="8",Text="8"},
                new SelectListItem(){Value="18",Text="18"},
                new SelectListItem(){Value="20",Text="20"},
                new SelectListItem(){Value="21",Text="21"}
                };
            
            ///SelectList list = new SelectList(dt, "Lid", "LName");
            

                ViewBag.liAge = liAge;
                return View();
            }

            /// <summary>
            /// 添加
            /// </summary>
            /// <param name="stu"></param>
            /// <returns></returns>
            [HttpPost]
            public ActionResult Add(Student stu)
            {
                //获取api的方法
                Uri url = new Uri(address, "/api/Stu");
                //引用命名空间
                using (HttpClient client = new HttpClient())
                {
                    var result = client.PostAsJsonAsync(url, stu).Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return Content("<script>alert('添加成功');location.href='/StuMvc/Index';</script>");
                    }
                    else
                    {
                        return Content("<script>alert('添加失败')</script>");
                    }
                }
            }
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public ActionResult Delete(int id)
            {
                //获取api的方法
                Uri url = new Uri(address, "/api/Stu/"+id);
                //引用命名空间
                using (HttpClient client = new HttpClient())
                {
                    var result = client.DeleteAsync(url).Result;
                    //判断返回值
                    if (result.IsSuccessStatusCode)
                    {
                        return Content("<script>alert('删除成功');location.href='/StuMvc/Index';</script>");
                    }
                    else
                    {
                        return Content("<script>alert('删除失败')</script>");
                    }
                }
            }
            /// <summary>
            /// 查询单个值
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            //public ActionResult Upd(string id)
            //{
            //    Student list = new Student();
            //    Uri url = new Uri(address, "/api/stu/" + id);
            //    //引用命名空间
            //    using (HttpClient client = new HttpClient())
            //    {
            //        var result = client.GetAsync(url).Result;
            //        //判断返回值状态
            //        if (result.IsSuccessStatusCode)
            //        {
            //            list = result.Content.ReadAsAsync<Student>().Result;
            //        }

            //        return Json(list, JsonRequestBehavior.AllowGet);
            //    }
            //}

            /// <summary>
            /// 修改显示页面
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public ActionResult Upd(int id)
            {
                //自定义下拉列表
                List<SelectListItem> liAge = new List<SelectListItem>() {
                new SelectListItem(){Value="8",Text="8"},
                new SelectListItem(){Value="18",Text="18"},
                new SelectListItem(){Value="20",Text="20"},
                new SelectListItem(){Value="21",Text="21"}
                };
                ViewBag.liAge = liAge;
                //建立接口
                Day3Entities db = new Day3Entities();
                //取出要修改的数据
                var data = db.Students.Where(t => t.ID == id).FirstOrDefault();
                //创建学生类接收数据
                var model = new Student()
                {
                    Age = data.Age,
                    ID = id,
                    NAME = data.NAME,
                    Sex=data.Sex
                };
                return View(model);
            }

            /// <summary>
            /// 修改
            /// </summary>
            /// <param name="stu"></param>
            /// <returns></returns>
            [HttpPost]
            public ActionResult Upd(Student stu)
            {
                //获取api的方法
                Uri url = new Uri(address, "/api/Stu");
                //引用命名空间
                using (HttpClient client = new HttpClient())
                {
                    var res=client.PutAsJsonAsync(url,stu).Result;
                    if (res.IsSuccessStatusCode)
                    {
                        return Content("<script>alert('修改成功');location.href='/StuMvc/Index';</script>");
                    }
                    else
                    {
                        return Content("<script>alert('修改失败')</script>");
                    }
                }
            }

        }

    ***********************************************************************************************************

    //添加页面
    <div>
            @using( Html.BeginForm())
            {
                <p>学生姓名:@Html.TextBoxFor(T => T.NAME)</p>
                <p>学生性别:@Html.RadioButtonFor(T=>T.Sex,"男")男
                         @Html.RadioButtonFor(T => T.Sex, "女")女
                </p>
                <p>学生年龄:@Html.DropDownListFor(T => T.Age, ViewBag.liAge as List<SelectListItem>)</p>

            //////////<p>@Html.DropDownListFor(T=>T.Lid,ViewBag.li as SelectList)</p>


                <p><input id="Submit1" type="submit" value="提交" /></p>
            }
           
     </div>


  • 相关阅读:
    20210329 3. RocketMQ 高级实战
    20210329 2. RocketMQ 高级特性及原理
    20210329 1. RocketMQ 架构与实战
    20210329 0. RocketMQ 安装
    20210311 java.io.Serializable
    Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments
    Reinforcement Learning in Continuous Time and Space
    A Learning Theory for Reward-Modulated Spike-Timing-Dependent Plasticity with Application to Biofeedback
    Functional Requirements for Reward-Modulated Spike-Timing-Dependent Plasticity
    BindsNET学习系列 ——Reward
  • 原文地址:https://www.cnblogs.com/lhn5xy/p/7908765.html
Copyright © 2020-2023  润新知