locked
Validation failed for one or more entities RRS feed

  • Question

  • User1912965948 posted

    Hello all,

    I'm facing problem to validate fields.

    I'm getting this error.

    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
    Line 34:             using (DBModel db = new DBModel()) {
    Line 35:                 db.Employees.Add(emp);
    Line 36:                 db.SaveChanges();
    Line 37:                 return Json(new { success = true, message = "Saved Successfuly" }, JsonRequestBehavior.AllowGet);
    Line 38:             }
    System.Data.Entity.Validation.DbEntityValidationException
      HResult=0x80131920
      Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
      Source=EntityFramework
      StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at MVCDataTable.Controllers.EmployeeController.AddEdit(Employee emp) in D:\Projects\MVCDataTable\MVCDataTable\Controllers\EmployeeController.cs:line 38
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

    Controller

    [HttpGet]
            public ActionResult AddEdit(int id = 0) {
                return View(new Employee());
            }
    
            [HttpPost]
            public ActionResult AddEdit(Employee emp) {
    
                using (DBModel db = new DBModel()) {
                    db.Employees.Add(emp);
                    db.SaveChanges();
                    return Json(new { success = true, message = "Saved Successfuly" }, JsonRequestBehavior.AllowGet);
                }
            }

    HTML

    <button class="btn btn-primary" style="margin-bottom: 10px;" onclick="PopupForm('@Url.Action("AddEdit", "Employee")')"><i class="fa fa-plus"></i> Add New</button>
    @using (Html.BeginForm("AddEdit", "Employee", FormMethod.Post, new { onsubmit = "return SubmitForm(this)"}))
    {
    
        @Html.HiddenFor(model => model.EmpId)
    
        <div class="form-group">
            @Html.LabelFor(model => model.EmpName, new { @class = "control-label" })
            @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor( model => model.EmpName)
        </div>
    
        <div class="form-group">
            <input type="submit" value="Submit" class="btn btn-primary" />
            <input type="reset" value="Reset" class="btn btn-warning" />
        </div>

    Script

    <script>
            function PopupForm(url) {
                var formDiv = $('<div/>');
                $.get(url)
                    .done(function (response) {
                        formDiv.html(response);
    
                        popup = formDiv.dialog({
                            autoOpen: true,
                            resizable: false,
                            title: 'Employee Details',
                            height: 500,
                            width: 700,
                            close: function () {
                                popup.dialog('destroy').remove();
                            }
                        });
                    });
            }
    
            function SubmitForm(form) {
    
                $.validator.unobtrusive.parse(form);
    
                if ($(form).valid()) {
    
                    $.ajax({
                        type: 'POST',
                        url: form.action,
                        data: form.serialize(),
                        success: function (data) {
                            if (data.success) {
                                popup.dialog('close');
                                dataTable.ajax.reload();
                            }
                        }
                    });
                }
                return false;
            }
    
    
    </script>

    I'll be very thankful if someone solve this problem.

    Thursday, November 28, 2019 6:11 PM

All replies

  • User475983607 posted

    I'll be very thankful if someone solve this problem.

    Use the Visual Studio debugger to view the EntityValidationErrors properties.  The community cannot see this information.  Only you can...

    Thursday, November 28, 2019 6:25 PM
  • User1912965948 posted

    Use the Visual Studio debugger to view the EntityValidationErrors properties.  The community cannot see this information.  Only you can...

    System.Data.Entity.Validation.DbEntityValidationException
      HResult=0x80131920
      Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
      Source=EntityFramework
      StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at MVCDataTable.Controllers.EmployeeController.AddEdit(Employee emp) in D:\Projects\MVCDataTable\MVCDataTable\Controllers\EmployeeController.cs:line 38
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

    Thursday, November 28, 2019 7:27 PM
  • User475983607 posted

    The community cannot see your code or reproduce this issue.   

    Learn how to use the Visual Studio debugger to view the EntityValidationErrors properties.  Or use proper exception handling to log the error.  

    https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert

    Friday, November 29, 2019 1:06 AM