视图:
1 @model RegisterViewModel 2 @{ 3 ViewData["Title"] = "Register"; 4 } 5 6 <h2>@ViewData["Title"]</h2> 7 8 <div class="row"> 9 <div class="col-md-4"> 10 <form asp-route-returnUrl="@ViewData["ReturnUrl"]" method="post"> 11 <h4>Create a new account.</h4> 12 <hr /> 13 <div asp-validation-summary="All" class="text-danger"></div> 14 <div class="form-group"> 15 <label asp-for="Email"></label> 16 <input asp-for="Email" class="form-control" /> 17 <span asp-validation-for="Email" class="text-danger"></span> 18 </div> 19 <div class="form-group"> 20 <label asp-for="Password"></label> 21 <input asp-for="Password" class="form-control" /> 22 <span asp-validation-for="Password" class="text-danger"></span> 23 </div> 24 <div class="form-group"> 25 <label asp-for="ConfirmPassword"></label> 26 <input asp-for="ConfirmPassword" class="form-control" /> 27 <span asp-validation-for="ConfirmPassword" class="text-danger"></span> 28 </div> 29 <button type="submit" class="btn btn-default">Register</button> 30 </form> 31 </div> 32 </div> 33 34 @section Scripts { 35 @await Html.PartialAsync("_ValidationScriptsPartial") 36 }
对应的控制器操作:
1 [HttpPost] 2 [AllowAnonymous] 3 [ValidateAntiForgeryToken] 4 public async Task<IActionResult> Register(RegisterViewModel model /*隐式模型绑定*/, string returnUrl = null) 5 { 6 var testModel = new TestRegisterModel(); 7 await TryUpdateModelAsync(testModel); //显示模型绑定,从请求的视图中更新当前模型对象 8 9 ViewData["ReturnUrl"] = returnUrl; 10 if (ModelState.IsValid) 11 { 12 var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; 13 var result = await _userManager.CreateAsync(user, model.Password); 14 if (result.Succeeded) 15 { 16 _logger.LogInformation("User created a new account with password."); 17 18 var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); 19 var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); 20 await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); 21 22 await _signInManager.SignInAsync(user, isPersistent: false); 23 _logger.LogInformation("User created a new account with password."); 24 return RedirectToLocal(returnUrl); 25 } 26 AddErrors(result); 27 } 28 29 // If we got this far, something failed, redisplay form 30 return View(model); 31 }
隐式绑定参数RegisterViewModel的定义:
1 public class RegisterViewModel 2 { 3 [Required] 4 [EmailAddress] 5 [Display(Name = "Email")] 6 public string Email { get; set; } 7 8 [Required] 9 [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] 10 [DataType(DataType.Password)] 11 [Display(Name = "Password")] 12 public string Password { get; set; } 13 14 [DataType(DataType.Password)] 15 [Display(Name = "Confirm password")] 16 [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 17 public string ConfirmPassword { get; set; } 18 }
显式绑定参数TestRegisterModel的定义:
1 public class TestRegisterModel 2 { 3 public string Sex { get; set; } 4 public string Email { get; set; } 5 }
- 当操作中有参数时,模型绑定会隐式的工作;如果操作中无参数,则可以使用UpdateModel()和TryUpdateModel()方法显式地调用模型绑定;
- MVC模型绑定根据参数类型的属性(比如:ID,Title等等),会对应的在请求的视图中寻找对应的相同name属性值的控件的值,并将其传递给该参数的属性上;如果表单中不存在请求中的属性值,则参数的该属性将被自动赋值为空(NULL);
原文地址:https://www.cnblogs.com/lishidefengchen/p/8150683.html
时间: 2024-10-07 00:05:51