代码之家  ›  专栏  ›  技术社区  ›  Josh

ASP.NET核心MVC 2.2.1中未提交的表单

  •  0
  • Josh  · 技术社区  · 5 年前

    我面临着在ASP.NET-CORE 2.2.1 MVC中提交表单的问题。我意识到当我单击提交按钮时,表单不会提交。我已经确认验证令牌在那里,并且我已经指示了操作路径。

    我从一个视图模型生成了表单。下面是视图模型

    public class SignUpViewModel
    {
        [Required]
        public string Surname { get; set; }
    
        [Required,Display(Name ="Other Names")]
        public string Othernames { get; set; }
    
        [EmailAddress,Required,Display(Name ="Email Address")]
        public string EmailAddress { get; set; }
    
        [Display(Name ="Phone Number"),DataType(DataType.PhoneNumber),MinLength(11,ErrorMessage ="Phone number should be a valid GSM Mobile number"),Required]
        public string PhoneNumber { get; set; }
    
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    
        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    
        public Gender Gender { get; set; }
    
        [Display(Name ="State of Residence"),Required]
        public int? ResidenceState { get; set; }
    
        [Display(Name ="Local Government of Residence")]
        public int? ResidenceLga { get; set; }
    
        [Display(Name ="Date of Birth"),DataType(DataType.Date)]
        public DateTime? DateOfBirth { get; set; }
    }
    

    表格在这里

    <form asp-action="signup" method="POST">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Surname" class="control-label"></label>
                <input asp-for="Surname" class="form-control" />
                <span asp-validation-for="Surname" class="text-danger"></span>
            </div>
           <div class="form-group">
                <label asp-for="Othernames" class="control-label"></label>
                <input asp-for="Othernames" class="form-control" />
                <span asp-validation-for="Othernames" class="text-danger"> 
    </span>
            </div>
            <div class="form-group">
                <label asp-for="EmailAddress" class="control-label"></label>
                <input asp-for="EmailAddress" class="form-control" />
                <span asp-validation-for="EmailAddress" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Password" class="control-label"></label>
                <input asp-for="Password" class="form-control" />
                <span asp-validation-for="Password" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ConfirmPassword" class="control-label"></label>
                <input asp-for="ConfirmPassword" class="form-control" />
                <span asp-validation-for="ConfirmPassword" class="text-danger"> 
       </span>
            </div>
            <div class="form-group">
                <label asp-for="Gender" class="control-label"></label>
                <select asp-for="Gender" class="form-control" asp- 
         items="@Html.GetEnumSelectList<Gender>()">
                    <option selected="selected" value="">--Select--</option>
                </select>
                <span asp-validation-for="Gender" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ResidenceState" class="control-label"></label>
                <select asp-for="ResidenceState" class="form-control" asp- 
     items="ViewBag.StateId">
      <option value="">-Select-</option>
      </select>
                <span asp-validation-for="ResidenceState" class="text-danger"> 
     </span>
            </div>
            <div class="form-group">
                <label asp-for="ResidenceLga" class="control-label"></label>
                <select asp-for="ResidenceLga" class="form-control"><option 
     value="">-Select State First-</option></select>
                <span asp-validation-for="ResidenceLga" class="text-danger"> 
    </span>
            </div>
            <div class="form-group">
                <label asp-for="DateOfBirth" class="control-label"></label>
                <input asp-for="DateOfBirth" class="form-control" />
                <span asp-validation-for="DateOfBirth" class="text-danger"> 
       </span>
            </div>
            <div class="form-group">
                <input type="submit" value="Register" class="btn btn-primary" />
            </div>
        </form>
    

    控制器如下:

    public IActionResult Signup()
        {
            ViewData["StateId"] = new SelectList(_context.States, "Id", "Name");
    
            return View();
        }
    
        [HttpPost]
        //[ValidateAntiForgeryToken]
        public async Task<ActionResult> Signup(SignUpViewModel model)
        {
            if (EmailExists(model.EmailAddress))
            {
                ModelState.AddModelError("", "This email address is already registered with us.");
            }
    
            if (ModelState.IsValid)
            {
                try
                {
                    Enrollee enrollee = new Enrollee {
                        Surname=model.Surname,
                        OtherNames=model.Othernames,
                        Email=model.EmailAddress,
                        Gender=model.Gender,
                        StateId=model.ResidenceState,
                        LgaId=model.ResidenceLga,
                        DateOfBirth=model.DateOfBirth
                    };
    
                    var result = await _userManager.CreateAsync(enrollee, model.Password);
                    if (result.Succeeded)
                    {
    
                        //send password the new staff's email
    
                        var body = "<p>Email From {0} ({1})</p><p>Message:</p><p>{2}</p>";
                        var baseurl = $"{Request.Scheme}://{Request.Host}{Request.PathBase}";
                        var loginurl = baseurl + "/User/Login";
                        var msg = "Congratulations! " + model.Othernames + "," + "<br />" + "You have just been registered on the eSchool portal." + "<br />" + "Use this email as your login username and the password you registered with to login to the portal:" + "<br />" + "<br />" + "Click <strong><a href='" + loginurl + "'>here</a></strong> to login" + "\r" + "\r";
    
                        var message = string.Format(body, "eSchool Portal", "registration@eschoolnigeria.com", msg);
                        await _emailSender.SendEmailAsync(model.EmailAddress, "Registration on eSchool portal", message);
    
                        return RedirectToAction(nameof(Signup));
                    }
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(string.Empty, 
    error.Description);
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("",e.Message.ToString());
                }
            }
            else
            {
                ModelState.AddModelError(string.Empty, "Something went wrong 
    with the process. Please retry");
            }
            return RedirectToAction(nameof(Signup));
    
        }
    

    我将感谢任何错误的指导,使表格不提交。

    谢谢你

    1 回复  |  直到 5 年前
        1
  •  0
  •   Josh Withee    5 年前

    在你 [HttpPost] Signup(...) 你总是返回的动作 RedirectToAction(nameof(Signup)) 这会使你以空白表格回到同一页