ASP.NET MVC5网站开发之用户添加和浏览2(七)(7)

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Add(AddUserViewModel userViewModel) { if (userManager.HasUsername(userViewModel.Username)) ModelState.AddModelError("Username","用户名已存在"); if (userManager.HasEmail(userViewModel.Email)) ModelState.AddModelError("Email", "Email已存在"); if (ModelState.IsValid) { Core.User _user = new Core.User(); _user.RoleID = userViewModel.RoleID; _user.Username = userViewModel.Username; _user.Name = userViewModel.Name; _user.Sex = userViewModel.Sex; _user.Password = Core.General.Security.SHA256(userViewModel.Password); _user.Email = userViewModel.Email; _user.RegTime = System.DateTime.Now; var _response = userManager.Add(_user); if (_response.Code == 1) return View("Prompt",new Prompt() { Title="添加用户成功", Message="您已成功添加了用户【"+ _response.Data.Username+ "("+ _response.Data.Name + ")】", Buttons= new List<string> {"<a href=https://www.jb51.net/article/\"" + Url.Action("Index", "User") + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"btn btn-default\">用户管理</a>", "<a href=https://www.jb51.net/article/\"" + Url.Action("Details", "User",new { id= _response.Data.UserID }) + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"btn btn-default\">查看用户</a>", "<a href=https://www.jb51.net/article/\"" + Url.Action("Add", "User") + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"btn btn-default\">继续添加</a>"} }); else ModelState.AddModelError("", _response.Message); } //角色列表 var _roles = new RoleManager().FindList(); List<SelectListItem> _listItems = new List<SelectListItem>(_roles.Count()); foreach (var _role in _roles) { _listItems.Add(new SelectListItem() { Text = _role.Name, Value = _role.RoleID.ToString() }); } ViewBag.Roles = _listItems; //角色列表结束 return View(userViewModel); }

2.5添加成功提示

UserController中[右键]添加视图-Prompt

ASP.NET MVC5网站开发之用户添加和浏览2(七)

@model Ninesky.Web.Models.Prompt @{ ViewBag.Title = "提示"; } @section SideNav{@Html.Partial("SideNavPartialView")} <ol> <li><span></span> @Html.ActionLink("首页", "Index", "Home")</li> <li> @Html.ActionLink("用户管理", "Index", "User")</li> </ol> @Html.Partial("PromptPartialView", Model)

2.6 添加提示消息局部视图

Ninesky.Web/Areas/Control/Views/Shared【右键】->添加->视图。视图名为PromptPartialView。

ASP.NET MVC5网站开发之用户添加和浏览2(七)

代码如下:

@model Ninesky.Web.Models.Prompt <div> <div><div>@Model.Title</div></div> <div> <p>@Html.Raw(Model.Message)</p> @if (Model.Buttons != null && Model.Buttons.Count > 0) { <p> @foreach (var item in Model.Buttons) { @Html.Raw(item + "&nbsp;&nbsp;") } </p> } </div> </div>

运行效果

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjyjpf.html