ASP.NET MVC第016天_註冊用戶資料查詢結果顯示與單筆編輯刪除
這裡因為DB存有的資料較少筆
我先擴增多一些
AccountController.cs
1 2 3 4 5 6 7 | insert into AspNetUsers (Id,Account,Region,IsFirstTimeRequest,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName) select NEWID(),'Jack','二林',IsFirstTimeRequest,'jack5566@gmail.com',EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,'Jack' from AspNetUsers where Id='33681b96-88bd-4ee3-be3f-be95b72f0ce7' insert into AspNetUsers (Id,Account,Region,IsFirstTimeRequest,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName) select NEWID(),'Mike','丈八斗',IsFirstTimeRequest,'mike4433@gmail.com',EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,'Mike' from AspNetUsers where Id='33681b96-88bd-4ee3-be3f-be95b72f0ce7' |
AccountController.cs
寫好查詢出用戶資料列表的Index Action
Edit Action (分為GET跟POST)
還有Delete Action (僅GET)
共四個Action Method
Edit Action (分為GET跟POST)
還有Delete Action (僅GET)
共四個Action Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | using AgricultureManagementSystem.Models; using AgricultureManagementSystem.ViewModels; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; namespace AgricultureManagementSystem.Controllers { public class AccountController : Controller { private UserManager _userManager; public UserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<UserManager>(); } private set { _userManager = value; } } // GET: Account [HttpGet] public ActionResult Index() { var users = UserManager.Users.ToList(); return View(users); } // GET: Edit [HttpGet] public ActionResult Edit(string id) { if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id)) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); } User _user = UserManager.FindById(id); if (_user == null) { return HttpNotFound(); } var editUserViewModel = new EditUserViewModel() { Account = _user.Account, Email = _user.Email, Region = _user.Region }; return View(editUserViewModel); } [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Edit(string id, EditUserViewModel editUserViewModel) { if (ModelState.IsValid && !string.IsNullOrEmpty(id)) { User _user = UserManager.FindById(id); _user.Account = editUserViewModel.Account; _user.Email = editUserViewModel.Email; _user.Region = editUserViewModel.Region; var result = await UserManager.UpdateAsync(_user); if (result.Succeeded) { return RedirectToAction("Index", "Account"); } AddErrors(result); } return View(editUserViewModel); } [HttpGet] public async Task<ActionResult> Delete(string id) { if (string.IsNullOrEmpty(id) || string.IsNullOrWhiteSpace(id)) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); } User _user = UserManager.FindById(id); if (_user == null) { return HttpNotFound(); } var result = await UserManager.DeleteAsync(_user); if (!result.Succeeded) { AddErrors(result); } return RedirectToAction("Index", "Account"); } private void AddErrors(IdentityResult identityResult) { foreach (var error in identityResult.Errors) { ModelState.AddModelError("", error); } } } } |
Account Index檢視(用於陳列出所有user資料)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | @{
ViewBag.Title = "系統使用者查詢編輯";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@model IEnumerable<AgricultureManagementSystem.Models.User>
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">使用者帳戶</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>帳號</th>
<th>信箱</th>
<th>外場</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<td>@Html.DisplayFor(item=> user.UserName)</td>
<td>@Html.DisplayFor(item => user.Email)</td>
<td>@Html.DisplayFor(item => user.Region)</td>
<td>
@Html.ActionLink("編輯", "Edit", new { id = user.Id })
@Html.ActionLink("刪除", "Delete", new { id = user.Id })
</td>
</tr>
}
@*<tr>
<td>Jason</td>
<td>jason486@gmail.com</td>
<td>萬和</td>
<td>
@Html.ActionLink("編輯", "Edit")
</td>
</tr>
<tr>
<td>Jenny</td>
<td>jenny5566@gmail.com</td>
<td>二林</td>
<td>
@Html.ActionLink("編輯", "Edit")
</td>
</tr>*@
</tbody>
</table>
</div>
</div>
</div>
|
Account Edit檢視(用於將存在於既有DB中特定某筆user資料回填到編輯畫面)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | @model AgricultureManagementSystem.ViewModels.EditUserViewModel
@{
ViewBag.Title = "使用者資料編輯";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="col-lg-12">
<div class="card shadow mb-4">
<div class="card-header py-3">
使用者資料編輯
</div>
<div class="card-body">
<div class="row">
<div class="col-lg-6">
@using (Html.BeginForm("Edit", "Account", FormMethod.Post, new { @role = "form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Account)
@Html.TextBoxFor(m => m.Account, new { @class = "form-control", placeholder = "Enter account" })
@*<label>帳號</label>
<input class="form-control" placeholder="Enter account">*@
</div>
<div class="form-group">
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Enter email" })
@*<label>信箱</label>
<input class="form-control" placeholder="Enter email">*@
</div>
<div class="form-group">
@Html.LabelFor(m => m.Region)
@{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem() { Text = "請選擇一個外場地名", Value = "", Selected = true });
items.Add(new SelectListItem() { Text = "萬合", Value = "萬合", Selected = false });
items.Add(new SelectListItem() { Text = "二林", Value = "二林", Selected = false });
items.Add(new SelectListItem() { Text = "山寮", Value = "山寮", Selected = false });
items.Add(new SelectListItem() { Text = "丈八斗", Value = "丈八斗", Selected = false });
items.Add(new SelectListItem() { Text = "梨頭厝", Value = "梨頭厝", Selected = false });
}
@Html.DropDownListFor(m => m.Region, items, new { @class = "form-control" })
@*<label>外場</label>
<select class="form-control">
<option>萬和</option>
<option>二林</option>
<option>田尾</option>
</select>*@
</div>
<button type="submit" class="btn btn-facebook">確認修改</button>
}
</div>
<!-- /.col-lg-6 (nested) -->
</div>
<!-- /.row (nested) -->
</div>
</div>
<!-- /.panel -->
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
|
Edit檢視這裡搭配對應要自己建立的ViewModel
EditUserViewModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace AgricultureManagementSystem.ViewModels { public class EditUserViewModel { [Required] [Display(Name = "帳號")] public string Account { get; set; } [Required] [EmailAddress] [Display(Name = "信箱")] public string Email { get; set; } [Required] [Display(Name = "外場")] public string Region { get; set; } } } |
這裡運行效果
點選Mike調整外場位置可成功更新
選擇特定某筆Jack作刪除





留言
張貼留言