为什么我的表单验证没有解雇?

时间:2017-04-28 17:59:19

标签: asp.net-mvc

我正在基于视图模型在视图中构建表单。 View模型在每个字段和验证规则上都有[必需],但在检查Model.IsValid时,即使所有表单字段都为空或为空,它仍然会返回true。这是视图:

@model CommunityWildlifeHabitat.ViewModel.CreateAdminViewModel
@{
    ViewBag.Title = "CreateAdmin";
}

<h2>Create Admin</h2>

@using (Html.BeginForm("CreateAdmin", "Admin", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-6 control-label" })
        <div class="col-md-6">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-md-6 control-label" })
        <div class="col-md-6">
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.LastName, new { @class = "col-md-6 control-label" })
        <div class="col-md-6">
            @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.LastName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-6 control-label" })
        <div class="col-md-6">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-6 control-label" })
        <div class="col-md-6">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.ConfirmPassword, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-5 col-md-6">
            <input type="submit" class="btn btn-habitat" value="Create Admin" />
        </div>
    </div>
}

这是我的ViewModel

  public class CreateAdminViewModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { 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; }

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

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

这是Controller get和post

 [Authorize]
    public ActionResult CreateAdmin(int? id)
    {
        var model = new CreateAdminViewModel();
        if (id == null) { return RedirectToAction("Index", "Communities"); }
        if (User.Identity.IsAuthenticated)
        {
            var userId = User.Identity.GetUserId();
            var superUser = db.CommunityTeams.Where(x => x.UserId == userId && x.RoleId == 4).Any();
            // If User is not a SuperUser (Administrator)
            if (superUser == false)
                return RedirectToAction("Index", "Communities");
        }

    return View(model);

}

[Authorize]
[HttpPost]
public ActionResult CreateAdmin(FormCollection fc)
{
    var model = new CreateAdminViewModel();
    model.Email = fc["Email"];
    model.FirstName = fc["FirstName"];
    model.LastName = fc["LastName"];
    model.Password = fc["Password"];
    model.ConfirmPassword = fc["ConfirmPassword"];

    if (ModelState.IsValid)
    { }
    return RedirectToAction("index", "Admin");
}

1 个答案:

答案 0 :(得分:0)

在你的HttpPost上你有一个FormCollection的模型类型

public ActionResult CreateAdmin(FormCollection fc)

它应该是您的型号吗?其他总是真实的状态?

public ActionResult CreateAdmin(CreateAdminViewModel fc)