表单未在ASP.NET Core MVC 2.2.1中提交

时间:2019-02-11 13:47:24

标签: c# asp.net-core-mvc form-submit asp.net-core-2.2

我在以ASP.NET-core 2.2.1 MVC提交表单时遇到问题。当我单击“提交”按钮时,我意识到该表单未提交。我已经确认了验证令牌,并且已经指明了操作路径。

我从ViewModel生成了表单。这是下面的视图模型

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 个答案:

答案 0 :(得分:0)

在您的[HttpPost] Signup(...)操作中,您总是返回RedirectToAction(nameof(Signup)),这将导致您以空白表格返回同一页面