I'm trying to develop a simple website using asp.net mvc4
& EF6
where I can simply save a client's data in MS SQL 2012 DB
. For some unknown reason, whenever I try to save data I'm getting an error like this,
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
Here are my codes below,
Controller
[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");
}
}
Model
public class ClManagement
{
public ClientInfo AddUserInfo { get; set; }
public IEnumerable<ClientInfo> UserCollection { get; set; }
}
View
@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>
}
How can I solve this problem? What could possibly go wrong? Your help would be a life saving for me. Thanks!