Asp.net mvc 为什么我的表单验证没有启动?
我正在基于视图模型在视图中构建表单。视图模型在每个字段和验证规则上都有[Required],但在检查model.IsValid时,即使所有表单字段都为空或null,它也会不断返回true。以下是视图:Asp.net mvc 为什么我的表单验证没有启动?,asp.net-mvc,Asp.net Mvc,我正在基于视图模型在视图中构建表单。视图模型在每个字段和验证规则上都有[Required],但在检查model.IsValid时,即使所有表单字段都为空或null,它也会不断返回true。以下是视图: @model CommunityWildlifeHabitat.ViewModel.CreateAdminViewModel @{ ViewBag.Title = "CreateAdmin"; } <h2>Create Admin</h2> @using (Ht
@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>
}
这是控制器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");
}
在HttpPost上,您有一个FormCollection的模型类型
public ActionResult CreateAdmin(FormCollection fc)
它应该是你的型号吗?否则总是真实的状态吗
public ActionResult CreateAdmin(CreateAdminViewModel fc)
在HttpPost上,您有一个FormCollection的模型类型
public ActionResult CreateAdmin(FormCollection fc)
它应该是你的型号吗?否则总是真实的状态吗
public ActionResult CreateAdmin(CreateAdminViewModel fc)
你说得对,我把一切都弄明白了。我因为收集表格而感到困惑。我通过了模型,现在它工作了。你是对的,我把一切都弄明白了。我因为收集表格而感到困惑。我通过了模型,现在它正在工作。