public async Task <IActionResult> Create([Bind("username,password,sex,email,status,img,telephone,workplace,occupation,address")] UsersViewModel users) { ///<summary> /// 将ASP.NET Core MVC模型绑定器创建的user实体添加到users实体集,然后将更改保存到数据库 /// 模型绑定器指的是 ASP.NET Core MVC 功能,用户可利用它来轻松处理使用表单提交的数据 /// 模型绑定器将已发布的表单值转换为 CLR 类型,并将其传递给操作方法的参数 ///</summary> try { if (ModelState.IsValid) { _context.Add(users); //实体状态:Added,SaveChanges 方法发出 INSERT 语句 await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException /* ex */) { ///Log the error (uncomment ex variable name and write a log. /// 如果保存更改时捕获到来自 DbUpdateException 的异常,则会显示一般错误消息。 有时 DbUpdateException 异常是由应用程序外部的某些内容而非编程错误引起的,因此建议用户再次尝试 ModelState.AddModelError("", "新增用户失败" + "再次尝试,如果问题仍然存在" + "请联系系统管理者."); } return(View(users)); }
public async Task <IActionResult> Create([Bind("title,author,source,fb_time,wz_content,wz_style,state,userID")] Science_essayViewModel science_essayViewModel) { if (ModelState.IsValid) { _context.Add(science_essayViewModel); await _context.SaveChangesAsync(); return(Content("<meta http-equiv='Content-Type' content='text/html; charset = UTF-8' /><script>var r=confirm('成功提交文章!继续提交……');if (r == true){history.go(-1);}else{location.href = 'Index';}</script>", "text/html")); //return RedirectToAction(nameof(Index)); } return(View(science_essayViewModel)); }
public async Task <IActionResult> Create([Bind("title,author,source,fb_time,wz_content,wz_style,state,userID")] Science_essayViewModel science_essayViewModel) { if (ModelState.IsValid) { _context.Add(science_essayViewModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["userID"] = new SelectList(_context.users, "ID", "username", science_essayViewModel.userID); return(View(science_essayViewModel)); }
public async Task <IActionResult> Create([Bind("ID,comment,comment_time,userID,essayID")] Forum_msgViewModel forum_msgViewModel) { if (ModelState.IsValid) { _context.Add(forum_msgViewModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["essayID"] = new SelectList(_context.Science_essay, "ID", "ID", forum_msgViewModel.essayID); //ViewData["userID"] = new SelectList(_context.users, "ID", "password", forum_msgViewModel.userID); ViewData["userID"] = new SelectList(_context.users, "ID", "userID", forum_msgViewModel.userID); return(View(forum_msgViewModel)); }
public async Task <IActionResult> Register([Bind("username,password,sex,email,status,telephone,img,workplace,occupation,address")] UsersViewModel users) { try { if (ModelState.IsValid) { _context.Add(users); await _context.SaveChangesAsync(); return(new ContentResult() { Content = "<meta http-equiv='Content-Type' content='text/html; charset = UTF-8' />" + "<div class='content' style='text-align:center; color:#52FA4D; letter-spacing:3px;'>注册成功!</div>" + "<script>" + " var content = document.getElementsByClassName('content')[0];" + " var timeLeft = 2;" + " function tiaozhuan(timeLeft) {" + " if (timeLeft > 0) {" + " setTimeout(function() {" + " timeLeft--;" + " content.innerText = timeLeft + '后将跳转到登录页面';" + " tiaozhuan(timeLeft);" + " },1000); " + " }" + " else { window.location.href = 'Login'; }" + " }" + " tiaozhuan(timeLeft);" + "</script> ", ContentType = "text/html", StatusCode = 200 }); } } catch (DbUpdateException /* ex */) { ModelState.AddModelError("", "注册失败" + "再次尝试,如果问题仍然存在" + "请联系系统管理者."); } return(RedirectToAction(nameof(TipsFailed))); }
public async Task <IActionResult> NewForumMsg([Bind("comment,comment_time,userID,essayID")] Forum_msgViewModel forum_msgViewModel, string essayID, string comment) { try { if (ModelState.IsValid) { if (comment == null) { return(Content("<meta http-equiv='content-type' content='text/html; charset = utf-8' /><script>alert('请输入评论内容');history.go(-1);</script>", "text/html")); } _context.Add(forum_msgViewModel); await _context.SaveChangesAsync(); return(Content("<meta http-equiv='content-type' content='text/html; charset = utf-8' /><script>alert('发表成功');history.go(-1);</script>", "text/html")); } } catch (DbUpdateException /* ex */) { ModelState.AddModelError("", "发布信息失败" + "再次尝试,如果问题仍然存在" + "请联系系统管理者."); } return(View(forum_msgViewModel)); }