public ActionResult RegisterPost(HttpPostedFileBase Photo) { var item = new Student() { UserRole = AccountStatus.Student }; try { TryUpdateModel(item, "RegisterItem", new string[] { "Login", "Name", "Password", "Group", "Course", "Age" }); if (db.Users.Count(i => i.Login == item.Login) > 0) throw new ArgumentException("Пользователь с таким e-mail уже существует."); if (ModelState.IsValid) { if (Photo != null && Photo.IsImage()) { item.PhotoMime = Photo.ContentType; item.Photo = new WebImage(Photo.InputStream).Resize(300, 300).GetBytes(item.PhotoMime); Photo.InputStream.Read(item.Photo, 0, Photo.ContentLength); } db.Students.Add(item); db.SaveChanges(); System.Web.HttpContext.Current.Session.Authorize(item); return RedirectToAction("Profile"); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } return View(new AccountViewModel(item)); }
public ActionResult EditPost(int? id) { User user = null; try { if (id.HasValue) { user = db.Users.SingleOrDefault(i => i.Id == id); if (user == null) throw new ArgumentException("Пользователь не найден."); } else { user = new User(); TryUpdateModel(user, "Item", new[] { "UserRole" }); if (user.UserRole == AccountStatus.Admin) user = new Administrator(); if (user.UserRole == AccountStatus.Student) user = new Student(); if (user.UserRole == AccountStatus.Teacher) user = new Teacher(); TryUpdateModel(user as User, "Item", new[] { "Password", "UserRole" }); } TryUpdateModel(user as User, "Item", new[] { "Login", "Name" }); if (user.UserRole == AccountStatus.Admin) TryUpdateModel(user as Administrator, new[] { "Telephone", "Email" }); if (user.UserRole == AccountStatus.Student) TryUpdateModel(user as Student, new[] { "Age", "Group", "Course" }); if (user.UserRole == AccountStatus.Teacher) TryUpdateModel(user as Teacher, new[] { "University", "Department" }); #warning Да простит меня Родионов за ЭТО... if (true)//ModelState.IsValid) { if (id == null) { if (db.Users.Count(i => i.Login == user.Login) > 0) throw new ArgumentException("Пользователь с таким e-mail уже существует."); if (user.UserRole == AccountStatus.Admin) db.Administrators.Add(user as Administrator); if (user.UserRole == AccountStatus.Student) db.Students.Add(user as Student); if (user.UserRole == AccountStatus.Teacher) db.Teachers.Add(user as Teacher); } else { if (user.UserRole == AccountStatus.Admin) db.Entry<Administrator>(user as Administrator).State = EntityState.Modified; if (user.UserRole == AccountStatus.Student) db.Entry<Student>(user as Student).State = EntityState.Modified; if (user.UserRole == AccountStatus.Teacher) db.Entry<Teacher>(user as Teacher).State = EntityState.Modified; } db.SaveChanges(); return RedirectToAction("Index"); } } catch (Exception ex) { #warning И за это пусть он меня простит... ModelState.Clear(); ModelState.AddModelError(string.Empty, ex.Message); } return View(new UserViewModel(db, user)); }
public AccountViewModel(Student item) { RegisterItem = item; }