提交表单总是调用get action方法而不是post action方法

时间:2015-06-05 13:51:09

标签: c# asp.net-mvc asp.net-mvc-5

您好我的控制器(asp.net 4.5.1 mvc 5)中有以下代码,允许用户在我的网站上注册。一切都工作正常,但我添加了另一个控制器和另一个服务,现在,当我尝试注册所有它在提交时再次重定向到表单空白。调试后,下面的控制器中的post action方法永远不会被调用

await Task.WhenAll(list.Select(x => ProcessRequestAsync(x)));

相反,每次提交表单时都会调用下面的get动作方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, model.FirstName));
            var service = new GRCMemberService(HttpContext.GetOwinContext().Get<ApplicationDbContext>());
            service.CreateGRCMember(model.FirstName, model.LastName, model.Address1, model.Address2, model.City, model.County, model.Postcode, model.Telephone, model.DateOfBirth, model.Dietary, model.CompLicenceNo, model.SelectedLicenceTypeId, model.NOKFirstName, model.NOKLastName, model.NOKTelephone, model.RelationshipTypeId, model.OtherOrgsGRC, model.OtherClubEvents, model.OtherOrgsOutside, user.Id);
            //var currentUser = UserManager.FindByName(user.Id);
            //var newrole = ("GRCMember");
            //var roleresult = UserManager.AddToRole(currentUser.Id, newrole);
            await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

            // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
            // Send an email with this link
            // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
            // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            return RedirectToAction("Index", "Home");
        }  
        AddErrors(result);
    }
    // If we got this far, something failed, redisplay form 
    return View(model); 
}

知道什么是错的吗?

下面是我的ViewModel和View代码。

视图模型

[AllowAnonymous]
public ActionResult Register()
{
    return View();
}

查看

public class RegisterViewModel
{
    [Required]
    [Display(Name = "First Name")]
    [StringLength(160, ErrorMessage = "First Name cannot be longer than 160 characters.")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    [StringLength(160, ErrorMessage = "Last Name cannot be longer than 160 characters.")]
    public string LastName { get; set; }

    [Required]
    [Display(Name = "Address 1")]
    [StringLength(160, ErrorMessage = "Address1 cannot be longer than 160 characters.")]
    public string Address1 { get; set; }

    [Display(Name = "Address 2")]
    [StringLength(160, ErrorMessage = "Address2 cannot be longer than 160 characters.")]
    public string Address2 { get; set; }

    [Required]
    [Display(Name = "City")]
    [StringLength(100, ErrorMessage = "City cannot be longer than 100 characters.")]
    public string City { get; set; }

    [Display(Name = "County")]
    [StringLength(100, ErrorMessage = "County cannot be longer than 100 characters.")]
    public string County { get; set; }

    [Required]
    [Display(Name = "PostCode")]
    [StringLength(10, ErrorMessage = "Postcode cannot be longer than 10 characters.")]
    public string Postcode { get; set; }

    [Required]
    [Display(Name = "Telephone")]
    [StringLength(20, ErrorMessage = "Telephone cannot be longer than 20 characters.")]
    public string Telephone { get; set; }

    [StringLength(20, ErrorMessage = "Competition Licence Number cannot be longer than 20 characters.")]
    [Display(Name = "Licence Number")]
    public string CompLicenceNo { get; set; }

    [Display(Name = "Licence Type")]
    public int? SelectedLicenceTypeId { get; set; }

    [StringLength(200, ErrorMessage = "Competition Licence Number cannot be longer than 20 characters.")]
    [Display(Name = "Dietary Requirements - For events")]
    public string Dietary { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Next of Kin Name cannot be longer than 100 characters.")]
    [Display(Name = "Next of kin First Name")]
    public string NOKFirstName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Next of Kin Name cannot be longer than 100 characters.")]
    [Display(Name = "Next of kin Last Name")]
    public string NOKLastName { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "Next of Kin Telephone cannot be longer than 20 characters.")]
    [Display(Name = "Next of kin Telephone")]
    public string NOKTelephone { get; set; }

    [Display(Name = "Next of kin Relationship")]
    public int? RelationshipTypeId { get; set; }

    [Required]
    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime DateOfBirth { get; set; }

    [Display(Name = "Allow other organisations on Grass Roots Clicks to contact you?")]
    public bool OtherOrgsGRC { get; set; }

    [Display(Name = "Allow other clubs you are a member of or ones whose events you enter to contact you?")]
    public bool OtherClubEvents { get; set; }

    [Display(Name = "Allow other organisations outside of Grass Roots Clicks that we are working with to contact you?")]
    public bool OtherOrgsOutside { get; set; }

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [EmailAddress]
    [Display(Name = "Confirm email")]
    [Compare("Email", ErrorMessage = "Your email and confirmation email do not match.")]
    public string ConfirmEmail { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} 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; }
}

1 个答案:

答案 0 :(得分:2)

我通过删除视图开头的表单,字段集和节标记来解决问题。

感谢ekad帮助调试问题

相关问题