ASP.NET MVC5网站开发之用户角色的后台管理1(七)(3)

ASP.NET MVC5网站开发之用户角色的后台管理1(七)

 

@model Ninesky.Core.Role @{ ViewBag.Title = "添加角色"; } @section SideNav{@Html.Partial("SideNavPartialView")} <ol> <li><span></span> @Html.ActionLink("首页", "Index", "Home")</li> <li>@Html.ActionLink("用户管理", "Index", "User")</li> <li>@Html.ActionLink("角色管理", "Index", "Role")</li> <li>添加角色</li> </ol> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div> @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" }) </div> </div> <div> <div> <input type="submit" value="保存" /> </div> </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

3)、添加提交数据的接收处理方法

在控制器中添加Add方法的post方法

[HttpPost] [ValidateAntiForgeryToken] public ActionResult Add(Role role) { if (ModelState.IsValid) { if (roleManager.Add(role).Code == 1) { return View("Prompt", new Prompt() { Title = "添加角色成功", Message ="你已成功添加了角色【"+ role.Name+"】", Buttons = new List<string>() { "<a href=https://www.jb51.net/article/\"" + Url.Action("Index", "Role") + "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", "Role") + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"btn btn-default\">继续添加</a>"} }); } } return View(role); }

5、管理员资料修改

1)、添加方法

在控制器中添加Modify方法。

/// <summary> /// 修改 /// </summary> /// <param>RoleID</param> /// <returns></returns> public ActionResult Modify(int id) { var _role = roleManager.Find(id); if(_role == null) return View("Prompt", new Prompt() { Title = "错误", Message = "ID为【" + id + "】的角色不存在", Buttons = new List<string>() { "<a href=https://www.jb51.net/article/\"" + Url.Action("Index", "Role") + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"btn btn-default\">角色管理</a>"} }); return View(_role); }

2)、添加视图

在方法中右键添加视图

ASP.NET MVC5网站开发之用户角色的后台管理1(七)

代码如下:

@model Ninesky.Core.Role @{ ViewBag.Title = Model.Name; } @section SideNav{@Html.Partial("SideNavPartialView")} <ol> <li><span></span> @Html.ActionLink("首页", "Index", "Home")</li> <li>@Html.ActionLink("用户管理", "Index", "User")</li> <li>@Html.ActionLink("角色管理", "Index", "Role")</li> <li>修改</li> </ol> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.RoleID) <div> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div> @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" }) </div> </div> <div> <div> <input type="submit" value="保存" /> </div> </div> </div> } @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

3)、添加提交数据的接收处理方法

在控制器中添加post方式的提交处理方法Modify方法。

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

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