You can always package the result and return JsonResult. The previous answer can be used for something like this:
[HttpPost]
public ActionResult Create(ResturantSet resturantset)
{
if (ModelState.IsValid)
{
try
{
db.ResturantSet.Add(resturantset);
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
var errorList = new List<string>();
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
errorList.Add(String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
}
}
return Json(errorList);
}
return RedirectToAction("Index");
}
return View(resturantset);
}
Then inside you jQuery code use $.each() method to process the string. Also, please notice that I did not test this code, but I just want to give you an idea how to get it done. Also, the above will work if you POSTed the request through one of jQuery's
Ajax methods like $.ajax or $.post.
The validation should occur during model binding, and is saved in the ModelState. When you check if ModelState.IsValid, it should know whether there will be any entity validation errors. If this is working, the client-side validation should "just work"
How are you defining your validation rules? Can we see your RestaurantSet model?
public partial class ResturantSet
{
public ResturantSet()
{
this.MenuSet = new HashSet<MenuSet>();
}
public int Id { get; set; }
public string Navn { get; set; }
public string Kontaktperson { get; set; }
public string Lokation { get; set; }
public string Info { get; set; }
public string SoegeThumbnail { get; set; }
public string ResturantBillede { get; set; }
public virtual ICollection<MenuSet> MenuSet { get; set; }
}
Please try adding some validation attributes to your ReataurantSet.cs class
public partial class ResturantSet
{
public ResturantSet()
{
this.MenuSet = new HashSet<MenuSet>();
}
public int Id { get; set; }
[Required]
public string Navn { get; set; }
[Required]
[MaxLength(255)]
public string Kontaktperson { get; set; }
public string Lokation { get; set; }
public string Info { get; set; }
public string SoegeThumbnail { get; set; }
public string ResturantBillede { get; set; }
public virtual ICollection<MenuSet> MenuSet { get; set; }
}
The validation attriubutes are in the System.ComponentModel.DataAnnotations namespace, so make sure to include it in your model.
After you do this, the model validator should be able to figure out how to validate your objects, and ModelState.IsValid should work properly. This works for code-first, so I'm not 100% sure if it will work for you, but give it a try.
Here is an article on using data annotations in your models.
Bormeth
Member
67 Points
85 Posts
Validation failed for one or more entities. :)
Jan 25, 2013 09:52 PM|LINK
Hey Guys :) Tried to do some google'ing but havnt helped yet.
I've made a entity datamodel generated from my database. Afterwards added a controler bound to it.
A simple create function gives me "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."
The method
[HttpPost] public ActionResult Create(ResturantSet resturantset) { if (ModelState.IsValid) { db.ResturantSet.Add(resturantset); db.SaveChanges(); return RedirectToAction("Index"); } return View(resturantset); }This is already added
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
How can i fire the validation? :)
mgasparel
Member
298 Points
65 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 06:30 AM|LINK
You can use a try/catch block to inspect the entity validation errors:
[HttpPost] public ActionResult Create(ResturantSet resturantset) { if (ModelState.IsValid) { try { db.ResturantSet.Add(resturantset); db.SaveChanges(); } 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("Index"); } return View(resturantset); }Note: dbEntityValidationException is in the System.Data.Entity.Validation namespace
Bormeth
Member
67 Points
85 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 09:55 AM|LINK
I would like them to happen on the client-side via. jQuery, any possible solution ?
Huske
Contributor
4060 Points
756 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 12:36 PM|LINK
You can always package the result and return JsonResult. The previous answer can be used for something like this:
[HttpPost] public ActionResult Create(ResturantSet resturantset) { if (ModelState.IsValid) { try { db.ResturantSet.Add(resturantset); db.SaveChanges(); } catch (DbEntityValidationException dbEx) { var errorList = new List<string>(); foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { errorList.Add(String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage)); } } return Json(errorList); } return RedirectToAction("Index"); } return View(resturantset); }Then inside you jQuery code use $.each() method to process the string. Also, please notice that I did not test this code, but I just want to give you an idea how to get it done. Also, the above will work if you POSTed the request through one of jQuery's Ajax methods like $.ajax or $.post.
mgasparel
Member
298 Points
65 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 03:48 PM|LINK
I think we are moving backwards here...
The validation should occur during model binding, and is saved in the ModelState. When you check if ModelState.IsValid, it should know whether there will be any entity validation errors. If this is working, the client-side validation should "just work"
How are you defining your validation rules? Can we see your RestaurantSet model?
Bormeth
Member
67 Points
85 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 05:29 PM|LINK
First thx for all the answers guys! :)
This is the ResturantSet
The SQL which creates it. :) The Model is made straight from it without any changes.
Erik.SM
Member
332 Points
61 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 05:45 PM|LINK
public class MyViewModel { [Required] public string FirstName {get;set;} [Required] public string LastName {get;set;} ... etc. }mgasparel
Member
298 Points
65 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 05:48 PM|LINK
I've only ever used the code-first approad for the Entity Framework.
Does your project generate a model class (RestaurantSet.cs) from that table? If so, can you post that code?
Bormeth
Member
67 Points
85 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 06:39 PM|LINK
This is the ResturantSet.Cs it made for me
public partial class ResturantSet { public ResturantSet() { this.MenuSet = new HashSet<MenuSet>(); } public int Id { get; set; } public string Navn { get; set; } public string Kontaktperson { get; set; } public string Lokation { get; set; } public string Info { get; set; } public string SoegeThumbnail { get; set; } public string ResturantBillede { get; set; } public virtual ICollection<MenuSet> MenuSet { get; set; } }mgasparel
Member
298 Points
65 Posts
Re: Validation failed for one or more entities. :)
Jan 26, 2013 07:30 PM|LINK
Please try adding some validation attributes to your ReataurantSet.cs class
public partial class ResturantSet { public ResturantSet() { this.MenuSet = new HashSet<MenuSet>(); } public int Id { get; set; } [Required] public string Navn { get; set; } [Required] [MaxLength(255)] public string Kontaktperson { get; set; } public string Lokation { get; set; } public string Info { get; set; } public string SoegeThumbnail { get; set; } public string ResturantBillede { get; set; } public virtual ICollection<MenuSet> MenuSet { get; set; } }The validation attriubutes are in the System.ComponentModel.DataAnnotations namespace, so make sure to include it in your model.
After you do this, the model validator should be able to figure out how to validate your objects, and ModelState.IsValid should work properly. This works for code-first, so I'm not 100% sure if it will work for you, but give it a try.
Here is an article on using data annotations in your models.
http://blogs.msdn.com/b/efdesign/archive/2010/03/30/data-annotations-in-the-entity-framework-and-code-first.aspx