public void CreateTutorInformation(User tutor, ITutor information) { Message = ""; Succeed = false; using (var db = new BaseDbContext()) { var contextUser = db.Users.Find(tutor.Id); if (contextUser == null) { Message = "未能从数据库中找到用户"; return; } var info = new TutorInformation { Tutor = contextUser, Introduction = information.TutorIntroduction, Position = information.TutorPosition, Avatar = information.TutorAvatar }; db.TutorInformations.Add(info); try { db.SaveChanges(); } catch (Exception) { Message = "出现错误"; return; } Succeed = true; return; } }
public async System.Threading.Tasks.Task<ActionResult> TutorCreate(CreateTutorViewModel model) { if (ModelState.IsValid) { if (Request.Files.Count != 1)//如果文件列表为空则返回 { ViewBag.Alert = "请检查上传文件!"; return View(); } var file = Request.Files[0];//只上传第一个文件 var user = Models.User.Create(model.Email, model.DisplayName); var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //为账户添加角色 var roleName = "Tutor"; ApplicationRoleManager roleManager = new ApplicationRoleManager(new RoleStore<IdentityRole>(db)); //判断角色是否存在 if (!roleManager.RoleExists(roleName)) { //角色不存在则建立角色 await roleManager.CreateAsync(new IdentityRole(roleName)); } //将用户加入角色 await UserManager.AddToRoleAsync(user.Id, roleName); // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771 // 发送包含此链接的电子邮件 // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户"); var avatar = Material.Create(user.DisplayName, MaterialType.Avatar, file, db); if (avatar == null) { TempData["ALert"] = "请检查上传文件!"; return View(model); } var tutor = new TutorInformation { Id = Guid.NewGuid(), Tutor = db.Users.Find(user.Id), Avatar = avatar, Position = model.Position, Introduction = model.Introduction }; db.TutorInformations.Add(tutor); db.SaveChanges(); ViewBag.Alert = "操作成功!"; return View(); } } ViewBag.Alert = "操作失败!"; return View(); }