मुझे एएसपी.नेट-कोर 2.2.1 एमवीसी में एक फॉर्म जमा करने में समस्या का सामना करना पड़ रहा है। मुझे एहसास हुआ कि जब मैं सबमिट बटन पर क्लिक करता हूं तो फॉर्म जमा नहीं होता है। मैंने पुष्टि की है कि सत्यापन टोकन है और मैंने कार्रवाई पथ का संकेत दिया है।

मैंने व्यूमोडेल से फॉर्म जेनरेट किया। यहाँ नीचे दृश्य मॉडल है

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
Josh 11 फरवरी 2019, 16:47

1 उत्तर

सबसे बढ़िया उत्तर

अपनी [HttpPost] Signup(...) कार्रवाई में आप हमेशा RedirectToAction(nameof(Signup)) लौट रहे हैं, जिसके कारण आप एक खाली फॉर्म के साथ उसी पृष्ठ पर वापस आ जाएंगे।

0
Josh Withee 11 फरवरी 2019, 20:19