码迷,mamicode.com
首页 > Windows程序 > 详细

naAPImvc

时间:2017-11-23 08:26:40      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:oca   下拉   tor   mod   ado   script   inpu   button   删除   

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"}
            };
            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><input id="Submit1" type="submit" value="提交" /></p>
        }
       
 </div>


naAPImvc

标签:oca   下拉   tor   mod   ado   script   inpu   button   删除   

原文地址:http://www.cnblogs.com/jin68/p/7881181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!