为什么当我点击提交时它没有点击动作?

时间:2014-02-20 21:34:36

标签: asp.net asp.net-mvc razor-2

我制作了一个全新的MVC5主页并遵循了本教程:http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

我没有添加2个字段(HomeTown,BirthDate),而是添加了很多字段。

这是我在IdentityModel中修改的内容:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? GenderId { get; set; }
    public DateTime Birthday { get; set; }
    public string Email { get; set; }
    public int? Phone { get; set; }
    public string Homepage { get; set; }
    public string Image { get; set; }
    public string Address { get; set; }
    public int? ZipCode { get; set; }
    public string City { get; set; }
    public int? CountryId { get; set; }
    public DateTime? LastLogin { get; set; }
    public DateTime Created { get; set; }
    public bool Verified { get; set; }
}

这是我在AccountViewModels中修改的内容:

 public class RegisterViewModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { 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; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [DisplayName("Gender")]
    [Required]
    public int? GenderId { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Birthday { get; set; }

    [Required]
    [System.Web.Mvc.Remote("CheckEmail", "Account", ErrorMessage = "Email is already registered")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

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

这是我的注册视图:

@model MembershipTutorial.Models.RegisterViewModel
@{
    ViewBag.Title = "Register";
}

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.UserName)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.Password)
    </div>
</div>
<div class="form-group">
    @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ConfirmPassword)
    </div>
</div>


<div class="form-group">
    @Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.GenderId, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.GenderId, (IEnumerable<SelectListItem>)ViewBag.GenderId, String.Empty)
        @Html.ValidationMessageFor(model => model.GenderId)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Birthday, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Birthday)
        @Html.ValidationMessageFor(model => model.Birthday)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <div class="left-inner-addon">
            <i class="glyphicon glyphicon-envelope"></i>
            @Html.EditorFor(model => model.Email)
        </div>
        @Html.ValidationMessageFor(model => model.Email)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.ConfirmEmail, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.EditorFor(m => m.ConfirmEmail, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ConfirmEmail)
    </div>
</div>



<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" class="btn btn-default" value="Register" />
    </div>
</div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    <link href="/Content/bootstrap-datepicker.css" rel="stylesheet" />
    <script src="/Scripts/bootstrap-datepicker.js"></script>
}

如果我只是单击提交按钮而不输入任何内容,则会显示所有内容的验证。当我填写不同的字段时,验证错误消息会消失(因为它们应该),但是当我点击提交时。什么都没发生。我调试了它并在开始时设置了断点:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)

但永远不会被召唤。如果我将代码更改回100%默认值,则会再次调用它。我在这里搞砸了什么?希望它只会在我脸上抛出一个异常,这样我才能看出问题所在。

1 个答案:

答案 0 :(得分:0)

您是否连接了System.Web.Mvc.Remote(“CheckEmail”,“帐户”?