2

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!

Shihan Khan
  • 2,180
  • 4
  • 34
  • 67
  • What does ClientInfo look like? Does it have any validation attributes? Have you looked at this http://stackoverflow.com/questions/15820505/dbentityvalidationexception-how-can-i-easily-tell-what-caused-the-error for how to get a better error message? – Mant101 Aug 04 '15 at 13:25
  • 1
    I have seen this error before. It means the the entity you are attempting to add to the SQL table is invalid. If the table has a "Not Null" column and you are NOT passing a value for it then you will see this error. Make sure your datatypes match up also. – KevDevMan Aug 04 '15 at 15:48
  • If you use **Entity Framework** you can have a look at my answer on [Solution for “Validation failed for one or more entities. See 'EntityValidationErrors' property for more details](http://stackoverflow.com/questions/21486072/solution-for-validation-failed-for-one-or-more-entities-see-entityvalidatione/29031857#29031857). Hope this helps... – Murat Yıldız Jan 26 '16 at 23:13

0 Answers0