I got an error in ASP.NET MVC, the problem is in action method called Save
but I don't know where is it exactly and here is the below error
System.Data.Entity.Validation.DbEntityValidationException: 'Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.'
Customer controller
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Vidly.Context;
using Vidly.Models;
using Vidly.VewModels;
namespace Vidly.Controllers
{
public class CustomersController : Controller
{
private MyDbContext _contex;
public CustomersController()
{
_contex = new MyDbContext();
}
protected override void Dispose(bool disposing)
{
_contex.Dispose();
}
public IActionResult New()
{
var membershipTypes = _contex.MembershipTypes.ToList();
var ViewModel = new CustomerFormViewModel
{
MembershipTypes = membershipTypes
};
return View("CustomerForm",ViewModel);
}
[HttpPost]
public IActionResult Save(Customer customer)
{
if (customer.Id == 0)
_contex.Customers.Add(customer);
else
{
var customerInDb = _contex.Customers.Single(c => c.Id == customer.Id);
customerInDb.Name = customer.Name;
customerInDb.BirthDate = customer.BirthDate;
customerInDb.MembershipTypeId = customer.MembershipTypeId;
customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
}
// doing here my logic
_contex.SaveChanges();
return RedirectToAction("Index","customers");
}
public IActionResult Edit(int id)
{
var customer = _contex.Customers.SingleOrDefault(c => c.Id == id);
if (customer == null)
return Content("not found");
var ViewModel = new CustomerFormViewModel
{
customers = customer,
MembershipTypes = _contex.MembershipTypes.ToList()
};
return View("customerForm",ViewModel);
}
public IActionResult Index()
{
var customers = _contex.Customers.Include(c => c.MembershipType).ToList();
return View(customers);
}
public IActionResult Details(int id)
{
var customers = _contex.Customers.Include(c => c.MembershipType).SingleOrDefault(c => c.Id == id);
return View(customers);
}
}
}
CustomerForm
@model Vidly.VewModels.CustomerFormViewModel
@{
ViewData["Title"] = "New";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>New</h1>
@using (Html.BeginForm("Save", "customers"))
{
<div class="form-group">
@Html.LabelFor(m => m.customers.Name)
@Html.TextBoxFor(m => m.customers.Name, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.customers.BirthDate)
@Html.TextBoxFor(m => m.customers.BirthDate, "{0:d MMM yyyy}", new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.customers.MembershipTypeId)
@Html.DropDownListFor(m => m.customers.MembershipTypeId, new SelectList(Model.MembershipTypes, "Id", "Name"), "Select Membership Type", new { @class = "form-control" })
</div>
<div class="checkbox">
@*<input class="form-check-input" type="checkbox" value="" id="flexCheckChecked" checked>*@
<label>
@Html.CheckBoxFor(m => m.customers.IsSubscribedToNewsLetter) Subscribed To NewsLetter?
</label>
</div>
@Html.HiddenFor(m => m.customers.Id)
<button type="submit" class="btn btn-primary">Save</button>
}