MVC TO LINQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    //
    // 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();
    }
}

  

posted @   逊老头  阅读(244)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示