在我的应用程序中,如果我向模型属性添加注释,则 ModelState.IsValid 变为 false。如果我删除所有注释,那么它就变成了真的。当我添加注释时,它的注释确实有效,但即使字段为空,它仍然会进入代码,这不应该发生。我正在使用 mvc4 剃须刀。
我应用注释的模型
using System.ComponentModel.DataAnnotations;
namespace HRMDatabaseLayer.Entities
{
using System;
using System.Collections.Generic;
public partial class EmployeeDetail
{
public EmployeeDetail()
{
this.EmployeeContactDetails = new HashSet<EmployeeContactDetail>();
this.EmployeeDepartments = new HashSet<EmployeeDepartment>();
this.EmployeeEducations = new HashSet<EmployeeEducation>();
this.EmployeeExperiances = new HashSet<EmployeeExperiance>();
this.EmployeeFamilies = new HashSet<EmployeeFamily>();
this.EmployeeLanguages = new HashSet<EmployeeLanguage>();
this.EmployeeSkills = new HashSet<EmployeeSkill>();
}
public int Id { get; set; }
[Required(ErrorMessage = "First Name is Required")]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required(ErrorMessage = "Last Name is Required")]
public string LastName { get; set; }
[Required(ErrorMessage = "Employee Id is Required")]
public string EmployeeId { get; set; }
[Required(ErrorMessage = "Gender is Required")]
public string Gender { get; set; }
public string MaritalStatus { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Birth Date is Required")]
public System.DateTime DateOfBirth { get; set; }
public string IsAdmin { get; set; }
public byte[] Picture { get; set; }
[Required(ErrorMessage = "Password is Required")]
public string Password { get; set; }
public virtual ICollection<EmployeeContactDetail> EmployeeContactDetails { get; set; }
public virtual ICollection<EmployeeDepartment> EmployeeDepartments { get; set; }
public virtual ICollection<EmployeeEducation> EmployeeEducations { get; set; }
public virtual ICollection<EmployeeExperiance> EmployeeExperiances { get; set; }
public virtual ICollection<EmployeeFamily> EmployeeFamilies { get; set; }
public virtual ICollection<EmployeeLanguage> EmployeeLanguages { get; set; }
public virtual ICollection<EmployeeSkill> EmployeeSkills { get; set; }
}
}
这是我的看法
<div>
@using (Html.BeginForm("Login", "Login"))
{
<table>
<tr>
<td>
<h3>Login</h3>
</td>
</tr>
<tr>
<td>Employee Id :
</td>
<td>
@Html.EditorFor(model => @model.Employee.EmployeeId)
</td>
<td>
@Html.ValidationMessageFor(model => @Model.Employee.EmployeeId)
</td>
</tr>
<tr>
<td>
Password :
</td>
<td>
@Html.EditorFor(model => @Model.Employee.Password)
</td>
<td>
@Html.ValidationMessageFor(model => @Model.Employee.Password)
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value ="Login" />
</td>
</tr>
</table>
@Html.ValidationSummary(true)
}
<br />
</div>
这是我使用 ModelState.IsValid 的控制器代码
public ActionResult Login(LoginViewModel empDetails)
{
if (ModelState.IsValid)
{
if (ValidateUser(empDetails.Employee.EmployeeId, empDetails.Employee.Password))
{
Session["id"] = empRepository.GetEmpId(empDetails.Employee.EmployeeId);
FormsAuthentication.SetAuthCookie(empDetails.Employee.EmployeeId, false);
return RedirectToAction("Index", "User", new { area = "EmployeeDetails" });
}
else
{
ViewBag.LoginError = "The user name or password provided is incorrect.";
}
}
empDetails.Employee.Password = string.Empty;
return View("Index", empDetails);
}