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();
}
}
MVC TO LINQ的更多相关文章
- 二:MVC之LINQ查询语法
LINQ(Language Integrated Query)语言集成查询是一组用于c#和Visual Basic语言的扩展.它允许编写C#或者Visual Basic代码以操作内存数据的方式,查询数 ...
- mvc ef LINQ to Entities 不识别方法“Int32 Parse(System.String)”,因此该方法无法转换为存储表达式。
private sys_User GetUserInfo() { sys_User model = null; var userId = Convert.ToInt32(AccountHelper.G ...
- 探讨mvc下linq多表查询使用viewModel的问题
最近在开发mvc3的时候发现了一个问题,就是如何在view页面显示多表查询的数据,最简单的办法就是使用viewmodel了,以下本人使用viewmodel来实现多表查询的3中方法, 先贴代码再说: 1 ...
- 【C#】MVC+EF+LINQ 综合小项目
第一,创建数据库 create table category(id int primary key,name nvarchar(20)) create table news(id int primar ...
- mvc使用linq to sql进行sum统计遇到查询为null的问题
mvc linq to sql,linq to entity,sum,null 昨天写了段sum的统计语句, decimal sums sums = ( from fac in db.Apply wh ...
- MVC NPOI Linq导出Excel通用类
之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...
- MVC中Linq to sql创建数据模型
1.创建新的 SQL Server 数据库 点击”视图“-->“服务器资源管理器” ,打开 “服务器资源管理器” 窗口,如下图: 右键“数据连接”,选择“创建新的SQL Server 数据库”, ...
- ASP.NET MVC 解决LINQ表达式中的SqlMethods 未找到命名空间问题
右键项目属性下的引用: 添加引用: 搜索寻找——System.Data.Linq,然后添加成功,即可解决LINQ表达式中的SqlMethods 未找到命名空间问题
- MVC +EF+linq 多表联查
关于linq的多表联查效果的实现: 后台多表查询 内连接: SELECT [Extent2].[partID] AS [partID], [Extent1].[userName] AS [userN ...
- .NET MVC+ EF+LINQ 多表联查VIEW显示列表
1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...
随机推荐
- linux_安装_安装编译phantomjs 2.0的方法_转
项目中要对数据公式webkit渲染,phantmjs 2.0的效果好比1.9好不少. 安装过程中 坑比较多. 转载文章: phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有 ...
- HDU4405-Aeroplane chess(可能性DP需求预期)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 今天才知道css hack是什么
先来个冷笑话:一晚下班回家,一民警迎面巡逻而来.突然对我大喊:站住! 民警:int类型占几个字节? 我:4个. 民警:你可以走了. 我感到很诧异. 我:为什么问这样的问题? 民警:深夜还在街上走,寒酸 ...
- 蜗牛—Hibernate之初识配置
下载Hibernate的jar包 把下面jar文件考到项目lib下 然后在myeclipse中打开database的view视图创建一个新的数据库连接 接下来,配置连接,须要导入ORACLE的jar包 ...
- 2299 Ultra-QuickSort(归并)
合并排序第一次.连环画看着合并看着别人的博客的想法. http://poj.org/problem? id=2299 #include <stdio.h> #include <std ...
- Net社区虚拟大会
微软“.Net社区虚拟大会”dotnetConf2015:关键词:.NET 创新.开源.跨平台 去年 11 月的时候,微软开源了 .NET CoreFX,然后是今年 2 月份的 .NET CoreCL ...
- TCP通信中的大文件传送
TCP通信中的大文件传送 源码 (为节省空间,不包含通信框架源码,通信框架源码请另行下载) 文件传送在TCP通信中是经常用到的,本文针对文件传送进行探讨 经过测试,可以发送比较大的文件,比如1个G ...
- 异常处理 Access to the path is denied
在需要进行读写操作的目录下添加Network Service这个帐号, 由于在iis 6.0中,默认的应用程序池中的标示用的是Network Service, 所以在进程中是使用Network Ser ...
- webkit的几个属性
-webkit-text-size-adjust 1.当样式表里font-size<12px时,中文版chrome浏览器里字体显示仍为12px,这时可以用 html{-webkit-text-s ...
- asp.net中TextBox里面实现回车触发指定事件
原文:asp.net中TextBox里面实现回车触发指定事件 我在一个user_top用户控件里面做了个包括搜索的功能.然后再一个页面中添加这个用户控件.浏览时候在textbox里面输入搜索内容后.下 ...