zoukankan      html  css  js  c++  java
  • MVC TO LINQ

            //
            // GET: /Home/
            TestTryEntities Db = new TestTryEntities();
    
            public ActionResult Index()
            {
                return View();
            }
    
            /// <summary>
            /// 查询
            /// </summary>
            /// <returns></returns>
            public ActionResult GetStudent()
            {
                int pageIndex = 1;//页码
                int pageSize = 10;//每页条数
                //var data = Db.Student.OrderBy(r => r.ID)
                //    .Skip(pageSize * (pageIndex - 1)).Take(pageSize)
                //    .Select(r => new { r.ID, r.Name, r.Password });
    
                var data1 = (from r in Db.Student
                             join t in Db.Class on r.ClassID equals t.ID
                             orderby r.ID
                             select new { r.ID, r.Name, r.Password, t.ClassName })
                             .Skip(pageSize * (pageIndex - 1)).Take(pageSize);
    
                int total = Db.Student.Count();//总条数
                //构造成Json的格式传递
    
    
                var result = new { total = total, rows = data1 };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
    
            //添加
            public ActionResult Add()
            {
                var data = Db.Class.Select(r => new SelectListItem()
                {
                    Text = r.ClassName,
                    Value = SqlFunctions.StringConvert((double)r.ID)
                }).ToList();
    
                ViewData["Class"] = data;
                return View();
    
            }
            [HttpPost]
            public ActionResult Add(Model.Student model)
            {
                Db.Student.Add(model);
                Db.SaveChanges();
                return View("Index");
            }
    
            /// <summary>
            /// 修改
            /// </summary>
            /// <param name="ID"></param>
            /// <returns></returns>
            public ActionResult Edit(int ID)
            {
                var model = Db.Student.Where(r => r.ID == ID).Select(r => r).FirstOrDefault();
                ViewData["modelStudent"] = model;
    
    
                var data = Db.Class.Select(r => new SelectListItem()
                {
                    Text = r.ClassName,
                    Value = SqlFunctions.StringConvert((double)r.ID),
                    Selected = (r.ID == model.ClassID)
                }).ToList();
                ViewData["Class"] = data;
                return View(model);
            }
    
            [HttpPost]
            public ActionResult Edit(Model.Student model)
            {
                Db.Student.Attach(model);
                Db.Entry<Student>(model).State = System.Data.EntityState.Modified;
                
                if (Db.SaveChanges() > 0)
                {
                    return Content("OK");
                }
                else
                {
                    return Content("修改失败");
                }
            }
    
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="ID"></param>
            /// <returns></returns>
            public ActionResult Delete(int ID)
            {
                //var model = Db.Student.Where(r => r.ID == ID).Select(r => r).FirstOrDefault();
                //if (model == null)
                //{
                //  return Script("alert('验证失败!)");
                //  return View("Index");
                //}
                //Db.Student.Remove(model);
                //Db.SaveChanges();
    
                var model1 = new Model.Student() { ID = ID };
                if (model1 == null)
                {
                    return View("Index");
                }
                Db.Student.Attach(model1);
                Db.Entry<Student>(model1).State = System.Data.EntityState.Deleted;
                Db.SaveChanges();
    
                return View("Index");
            }
    
            public ActionResult AddMulti()
            {
    
    
                return View();
            }
    
            [HttpPost]
            public ActionResult AddMulti(Mvctry.Models.ClassStudent model)
            {
    
    
                return View();
            }
        }
    

      

  • 相关阅读:
    使用 asp.net mvc和 jQuery UI 控件包
    ServiceStack.Redis 使用教程
    HTC T8878刷机手册
    Entity Framework CodeFirst 文章汇集
    2011年Mono发展历程
    日志管理实用程序LogExpert
    使用 NuGet 管理项目库
    WCF 4.0路由服务Routing Service
    精进不休 .NET 4.0 (1) asp.net 4.0 新特性之web.config的改进, ViewStateMode, ClientIDMode, EnablePersistedSelection, 控件的其它一些改进
    精进不休 .NET 4.0 (7) ADO.NET Entity Framework 4.0 新特性
  • 原文地址:https://www.cnblogs.com/Aamir-Ye/p/4584709.html
Copyright © 2011-2022 走看看