我正在尝试使用asp.net mvc4
& EF6
我只需在MS SQL 2012 DB
中保存客户的数据即可。由于某些未知原因,每当我尝试保存数据时,我都会收到这样的错误,
一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性
以下是我的代码,
控制器
[HttpPost]
public ActionResult ClientManager(ClManagement ClTable)
{
if (Session["AdminNAME"] != null)
{
if (ModelState.IsValid)
{
var AddClient = ClTable.AddUserInfo;
try
{
db.ClientInfoes.Add(AddClient);
db.SaveChanges();
TempData["client_add_success"] = "Information Added Successfully!";
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
return RedirectToAction("ClientManager", new { ClPanelId = "AllCl" });
}
else
{
TempData["client_add_fail"] = "Error! Information Add failed!";
return RedirectToAction("ClientManager", new { ClPanelId = "AllCl" });
}
}
else
{
return RedirectToAction("AdminLogin");
}
}
模型
public class ClManagement
{
public ClientInfo AddUserInfo { get; set; }
public IEnumerable<ClientInfo> UserCollection { get; set; }
}
查看
@using (Html.BeginForm("ClientManager", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div class="editor-label">
<strong>Username</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.username, new { size = 50 })
@Html.ValidationMessageFor(a => a.AddUserInfo.username)
</div>
<div class="editor-label">
<strong>Password</strong>
</div>
<div class="editor-field">
@Html.PasswordFor(a => a.AddUserInfo.password, new { size = 50 })
@Html.ValidationMessageFor(a => a.AddUserInfo.password)
</div>
<div class="editor-label">
<strong>Email</strong>
</div>
<div class="editor-field">
@Html.TextBoxFor(a => a.AddUserInfo.email, new { size = 50 })
@Html.ValidationMessageFor(a => a.AddUserInfo.email)
</div>
<div class="editor-label">
<strong>Sex</strong>
</div>
<div class="editor-field">
@Html.DropDownListFor(a => a.AddUserInfo.sex, new List<SelectListItem>{
new SelectListItem() {Text = "Male", Value="Male"},
new SelectListItem() {Text = "Female", Value="Female"}
})
@Html.ValidationMessageFor(a => a.AddUserInfo.sex)
</div>
<div class="editor-label">
<strong>Blood Group</strong>
</div>
<div class="editor-field">
@Html.DropDownListFor(a => a.AddUserInfo.blood, new List<SelectListItem>{
new SelectListItem() {Text = "A+", Value="A+"},
new SelectListItem() {Text = "B+", Value="B+"},
new SelectListItem() {Text = "AB+", Value="AB+"},
new SelectListItem() {Text = "O+", Value="O+"},
new SelectListItem() {Text = "A-", Value="A-"},
new SelectListItem() {Text = "B-", Value="B-"},
new SelectListItem() {Text = "AB-", Value="AB-"},
new SelectListItem() {Text = "O-", Value="O-"}
})
@Html.ValidationMessageFor(a => a.AddUserInfo.blood)
</div><br />
<p><input type="submit" class="btn btn-info" value="Add" /></p>
}
我该如何解决这个问题?什么可能出错?你的帮助对我来说可以挽救生命。谢谢!