【问题标题】:Why when I click submit is it not hitting the action?为什么当我点击提交时它没有点击操作?
【发布时间】:2014-02-20 21:34:35
【问题描述】:

我制作了一个全新的 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 个字段(家乡、出生日期),还添加了很多字段。

这是我在 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% 默认值,那么它会再次被调用。我在这里搞砸了什么?希望它只会在我脸上抛出一个异常,以便我可以看到问题出在哪里。

【问题讨论】:

  • 你连接了 System.Web.Mvc.Remote("CheckEmail", "Account"?
  • 我遇到了 mvc4 和 bootstrap2 的问题。基本上客户端验证失败,但由于 DOM 结构,错误没有显示出来。并且控制方法没有被触发。
  • 非常感谢。问题是我从以前的项目中复制了字段,但忘记实现该远程方法。做出回答,我会将其标记为已接受。

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


【解决方案1】:

您是否连接了 System.Web.Mvc.Remote("CheckEmail", "Account"?

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2022-01-18
    • 2020-03-31
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多