public ActionResult ChangePassword(ChangePasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                string currentStu   = Session["currStu"] as string;
                string currentAdmin = Session["admin"] as string;
                if (string.IsNullOrEmpty(currentStu) == false)
                {
                    var stu = db.Students.SingleOrDefault(s => s.Id == model.Id);

                    if (stu != null)
                    {
                        if (stu.Password != model.OldPassword)
                        {
                            return(Json(new { code = 1, data = "原密码不正确" }));
                        }

                        stu.Password        = model.Password;
                        db.Entry(stu).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        return(Json(new { code = 0, data = "密码修改成功,请重新登录。" }));
                    }
                }
                if (string.IsNullOrEmpty(currentAdmin) == false)
                {
                    var admin = db.Teachers.SingleOrDefault(a => a.Id == model.Id);
                    if (admin != null)
                    {
                        if (admin.Password != model.OldPassword)
                        {
                            return(Json(new { code = 1, data = "原密码不正确" }));
                        }

                        admin.Password        = model.Password;
                        db.Entry(admin).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        return(Json(new { code = 0, data = "密码修改成功,请重新登录。" }));
                    }
                }
            }
            return(Json(new { code = 1, data = "密码修改失败" }));
        }
 public ActionResult Edit([Bind(Include = "Id,Username,Password,Email,EName,EFamily,NatinalCardPhoto,Job,JobTelephone,PostCode,Peygiri,PeygiriPhotoFilename,Name,Family,FatherName,NatinalCode,ShenasnameCode,Birthday,City,Adderess,Mobile,Telephone,AltTelephone,PhotoFilename,Degree,Level,Average,Gender,SeatNumber,RegisterOn,Total,IsPaied,PaiedOn,TraceNumber,ReferenceNumber,TransactionReferenceID,TransactionDate,LastDegrePhotoFilename,University,Major,Country,EventId")] RegisterForm registerForm)
 {
     if (ModelState.IsValid)
     {
         db.Entry(registerForm).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.EventId = new SelectList(db.Events, "Id", "Name", registerForm.EventId);
     return(View(registerForm));
 }
Beispiel #3
0
        /// <summary>
        /// 从上传的学生成绩Excel中导入学生的指定课程成绩信息
        /// </summary>
        /// <param name="path"></param>
        /// <param name="cid">课程编号</param>
        /// <returns></returns>
        private ImportReturnModel ImportScoresFromExcel(string path, Course course)
        {
            //int result = 0;//0:失败
            var result = new ImportReturnModel
            {
                Code   = 0,
                Others = new List <string>()
            };

            string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + path + "; Extended Properties=Excel 8.0;";

            using (OleDbConnection Connection = new OleDbConnection(connectionString))
            {
                DataTable dt = new DataTable();
                Connection.Open();
                using (OleDbCommand command = new OleDbCommand())
                {
                    command.Connection  = Connection;
                    command.CommandText = "SELECT * FROM [成绩表$]";
                    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                    adapter.Fill(dt);
                    foreach (DataRow dr in dt.Rows)
                    {
                        string number      = dr["学号"].ToString().Trim();
                        string studentName = dr["姓名"].ToString().Trim();
                        //string courseName = dr["课程"].ToString().Trim();
                        float.TryParse(dr["成绩"].ToString().Trim(), out float scoreValue);


                        //判断导入的学生成绩是否有对应学生信息。
                        var student = db.Students.SingleOrDefault(stu => stu.Number == number);
                        if (student == null)
                        {
                            result.Others.Add(number + studentName); //如果没有,则收集学生学号和姓名。
                            continue;
                        }

                        //查找该学生该门课程成绩是否存在,如果存在则更新数据,不存在则新增数据。
                        var score = db.Scores.SingleOrDefault(ss => ss.Student.Number == number && ss.Course.CourseName == course.CourseName);
                        if (score == null)
                        {
                            Score s = new Score
                            {
                                Student = student,
                                Course  = course,
                                Value   = scoreValue
                            };
                            db.Scores.Add(s);
                        }
                        else
                        {
                            score.Value = scoreValue;
                            db.Entry <Score>(score).State = System.Data.Entity.EntityState.Modified;
                        }
                    }
                    adapter.Dispose();
                    int i = db.SaveChanges();
                    if (i > 0)
                    {
                        result.Code = 1;        //1:成功
                    }
                }
            }
            return(result);
        }