1. 程式人生 > 實用技巧 >筆試考試系統 ____學生資訊修改

筆試考試系統 ____學生資訊修改

1.今日任務

學生資訊修改

控制器對應程式碼:

 1 using Exam.UI.Filter;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Mvc;
 7 using Exam.BLL;
 8 using Utility;
 9 using Exam.Model;
10 
11 namespace Exam.UI.Controllers
12 {
13     [AdminFilter]
14     public
class StudentController : Controller 15 { 16 // GET: Student 17 public ActionResult Index() 18 { 19 var name = Session[CommonFeild.SessionName] as Exam_User; 20 21 return View(name); 22 } 23 public ActionResult Main() 24 { 25
return View(); 26 } 27 public ActionResult MyInfo() 28 { 29 var currentuser= Session[CommonFeild.SessionName] as Exam_User; 30 return View(currentuser); 31 } 32 [HttpPost] 33 public ActionResult Edit(string uname, string
Name, string phone, string id) 34 { 35 Exam_User u = new Exam_User { UserID = System.Convert.ToInt32(id), RealName = Name, UserName = uname, Phone = phone }; 36 try 37 { 38 StudentMannerService.Update(u); 39 } 40 catch (Exception ex) 41 { 42 return Json(new { msg = "修改失敗" + ex, success = false }); 43 44 } 45 return Json(new { msg = "修改成功", success = true }); 46 47 } 48 49 50 } 51 }

Service層對應方法

 1 using PagedList;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using Exam.Model;
 8 using Exam.DAL;
 9 
10 namespace Exam.BLL
11 {
12     public class StudentMannerService
13     {
14         
15         public static Exam_User FindStudentByID(int id)
16         {
17             using (ExamSysDBContext dBContext = new ExamSysDBContext())
18             {
19 
20                 var data = dBContext.Exam_User.Where(x => x.UserID == id).FirstOrDefault();
21                 return data;
22             }
23         }
24     
25         public static int Update(Exam_User u)
26         {
27             using (ExamSysDBContext dBContext = new ExamSysDBContext())
28             {
29                 var data = dBContext.Exam_User.Where(x => x.UserID == u.UserID).FirstOrDefault();
30                 data.UserName = u.UserName;
31                 data.Phone = u.Phone;
32                 data.RealName = u.RealName;
33                 return dBContext.SaveChanges();
34             }                
35         }
36     }
37 }

3.遇到問題

4.解決方案