2种方式都可以更新成功,不过第一种更新成功后,我这里的显示总是有问题,不知道什么时候就出来了。很郁闷
1 /// <summary>
2 /// 推荐和取消
3 /// Author:isaac zhang
4 /// Date: 2012-04-11
5 /// </summary>
6 /// <param name="id">主键ID</param>
7 /// <param name="type">操作类型</param>
8 /// <returns>列表页面</returns>
9 [HttpPost]
10 public ActionResult News_TopOrNot(int id = 0, string ttype = "top")
11 {
12 NewInfo model = null;
13 using (UBIQ_DB_JDZXContext db = new UBIQ_DB_JDZXContext())
14 {
15 model = db.NewInfoes.AsNoTracking().First(c => c.NewID == id);
16 }
17 if (model == null)
18 {
19 return HttpNotFound();
20 }
21 using (UBIQ_DB_JDZXContext db = new UBIQ_DB_JDZXContext())
22 {
23 int tuijian = 1;
24 if (ttype != "top")
25 {
26 tuijian = 0;
27 }
28 model.IsTop = tuijian;
29 db.Database.ExecuteSqlCommand("Update NewInfo SET istop = @istop WHERE newid = @newid"
30 , new SqlParameter[] { new SqlParameter("@istop",model.IsTop),
31 new SqlParameter("@newid",model.NewID)});
32 //db.NewInfoes.Attach(model);
33 //db.Entry(model).State = EntityState.Modified;
34 //var S = db.Database.SqlQuery<NewInfo>("SELECT * FROM NewInfo", null);
35 //db.SaveChanges();
36 }
37 return View("~/Views/Administrator/NewsAdmin/News_Manage.cshtml");
38 }
2 /// 推荐和取消
3 /// Author:isaac zhang
4 /// Date: 2012-04-11
5 /// </summary>
6 /// <param name="id">主键ID</param>
7 /// <param name="type">操作类型</param>
8 /// <returns>列表页面</returns>
9 [HttpPost]
10 public ActionResult News_TopOrNot(int id = 0, string ttype = "top")
11 {
12 NewInfo model = null;
13 using (UBIQ_DB_JDZXContext db = new UBIQ_DB_JDZXContext())
14 {
15 model = db.NewInfoes.AsNoTracking().First(c => c.NewID == id);
16 }
17 if (model == null)
18 {
19 return HttpNotFound();
20 }
21 using (UBIQ_DB_JDZXContext db = new UBIQ_DB_JDZXContext())
22 {
23 int tuijian = 1;
24 if (ttype != "top")
25 {
26 tuijian = 0;
27 }
28 model.IsTop = tuijian;
29 db.Database.ExecuteSqlCommand("Update NewInfo SET istop = @istop WHERE newid = @newid"
30 , new SqlParameter[] { new SqlParameter("@istop",model.IsTop),
31 new SqlParameter("@newid",model.NewID)});
32 //db.NewInfoes.Attach(model);
33 //db.Entry(model).State = EntityState.Modified;
34 //var S = db.Database.SqlQuery<NewInfo>("SELECT * FROM NewInfo", null);
35 //db.SaveChanges();
36 }
37 return View("~/Views/Administrator/NewsAdmin/News_Manage.cshtml");
38 }
注释的是显示有问题的更新方式,2种更新是都可以成功的哦~~
1 /// <summary>
2 /// 封装JSON字符串
3 /// Author:isaac zhang
4 /// Date: 2012-04-10
5 /// </summary>
6 /// <param name="pageIndex">页码</param>
7 /// <param name="pageCount">单页数据数</param>
8 /// <param name="sortname">排序字段</param>
9 /// <param name="sortorder">排序方式</param>
10 /// <returns></returns>
11 public static string GetNewJson(Int32 pageIndex, Int32 pageCount, String sortname, String sortorder)
12 {
13 //取出记录总数
14 var count = DataHelper.db.View_NewsInfoManage
15 .OrderByDescending(o => o.NewID).ToList().Count;
16 List<View_NewsInfoManage> list = null;
17 //根据排序type取数据
18 if (sortorder.ToLower() == "desc")
19 {
20 //list = DataHelper.db.View_NewsInfoManage
21 // .OrderByDescending(o => o.NewID).ToPagedList(pageIndex, pageCount);
22 String sql = "SELECT * FROM View_NewsInfoManage order by " + sortname + " " + sortorder;
23 list = DataHelper.db.Database.SqlQuery<View_NewsInfoManage>(sql).Skip((pageIndex - 1) * pageCount).Take(pageCount).ToList();
24 //DataHelper.db.Database.SqlQuery<View_NewsInfoManage>("SELECT * FROM NewInfo", null);
25
26 }
27 else
28 {
29 list = DataHelper.db.View_NewsInfoManage
30 .OrderBy(o => o.NewID).ToPagedList(pageIndex, pageCount);
31 }
2 /// 封装JSON字符串
3 /// Author:isaac zhang
4 /// Date: 2012-04-10
5 /// </summary>
6 /// <param name="pageIndex">页码</param>
7 /// <param name="pageCount">单页数据数</param>
8 /// <param name="sortname">排序字段</param>
9 /// <param name="sortorder">排序方式</param>
10 /// <returns></returns>
11 public static string GetNewJson(Int32 pageIndex, Int32 pageCount, String sortname, String sortorder)
12 {
13 //取出记录总数
14 var count = DataHelper.db.View_NewsInfoManage
15 .OrderByDescending(o => o.NewID).ToList().Count;
16 List<View_NewsInfoManage> list = null;
17 //根据排序type取数据
18 if (sortorder.ToLower() == "desc")
19 {
20 //list = DataHelper.db.View_NewsInfoManage
21 // .OrderByDescending(o => o.NewID).ToPagedList(pageIndex, pageCount);
22 String sql = "SELECT * FROM View_NewsInfoManage order by " + sortname + " " + sortorder;
23 list = DataHelper.db.Database.SqlQuery<View_NewsInfoManage>(sql).Skip((pageIndex - 1) * pageCount).Take(pageCount).ToList();
24 //DataHelper.db.Database.SqlQuery<View_NewsInfoManage>("SELECT * FROM NewInfo", null);
25
26 }
27 else
28 {
29 list = DataHelper.db.View_NewsInfoManage
30 .OrderBy(o => o.NewID).ToPagedList(pageIndex, pageCount);
31 }
同样,注释部分是只要更新后,就不能读到数据库的结果,而是直接从缓存(我认为的。)取了,新增和删除都不会出现这问题。。。。。。。。。。。。。太纠结了。。。
不过现在使用直接SQL 执行,不存在了,因为每次都是从数据库去读数据了。...................