ASP.NET MVC中的实体验证错误 [英] Entity validation error in ASP.NET MVC
问题描述
单击AddNewPost页面时出现实体验证错误。通常,当您提交表单时会发生验证错误,但是当我单击实际提交表单的页面时,我收到实体验证错误。当我调试代码以检查哪些数据库实体出现问题。我看到需要三个实体。但是当我提交表单时,这些实体应该提示这样的错误。在进入表单页面时发生这些错误时可能的解决方案是什么。
我尝试了什么:
我得到的错误
'/'应用程序中的服务器错误。
一个或多个实体的验证失败。有关更多详细信息,请参阅'EntityValidationErrors'属性
Visual Studio调试图像
http://imgur.com/a/CbpEM
控制器索引和添加帖子
public ActionResult Index()
{
AdminObserver observer1 = new AdminObserver();
ActivityObserver observer2 = new ActivityObserver();
// ForumNotifier通知程序=新的ForumNotifier();
ForumNotfier notifier = new ForumNotfier();
notifier.Subscribe(observer1);
notifier.Subscribe(observer2);
return View();
//返回View();
}
public ActionResult AddPost(ForumPost post)
{
post.PostedOn = DateTime.Now;
using(AppDbContext db = new AppDbContext())
{
try {
db.ForumPosts.Add(post);
db.SaveChanges();
}
catch(DbEntityValidationException e)
{
foreach(e.EntityValidationErrors中的变种)
{
Console.WriteLine(类型为\\的实体状态\{1} \中的{0} \具有以下验证错误:,
eve.Entry.Entity.GetType()。Name,eve.Entry.State);
foreach(var ve in eve.ValidationErrors)
{
Console.WriteLine( - Property:\{0} \,错误:\{1} \ ,
ve.PropertyName,ve.ErrorMessage);
}
}
throw;
}
}
ViewBag.Message =发布成功!;
ForumNotfier notifier = new ForumNotfier();
notifier.Notify(post);
返回查看(索引,帖子);
}
模型中的Forumpost
名称空间observer.Models
{
[表(ForumPosts)]
公共类ForumPost
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get;组; }
[必需]
[StringLength(20)]
公共字符串UserName {get;组; }
[必需]
[StringLength(100)]
public string Title {get;组; }
[必需]
[StringLength(100)]
public string说明{get;组; }
[必需]
public DateTime PostedOn {get;组; }
}
AddPost查看
@using(Html。 BeginForm())
{
@ Html.AntiForgeryToken()
< div class =form-horizontal>
< h4> ForumPost< / h4>
< hr />
@ Html.ValidationSummary(true,,new {@class =text-danger})
< div class =form-group>
@ Html.LabelFor(model => model.UserName,htmlAttributes:new {@class =control-label col-md-2})
< div class =col-md- 10\" >
@ Html.EditorFor(model => model.UserName,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.UserName ,,新{@class =text-danger})
< / div>
< / div>
< div class =form-group>
@ Html.LabelFor(model => model.Title,htmlAttributes:new {@class =control-label col-md-2})
< div class =col-md- 10\" >
@ Html.EditorFor(model => model.Title,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.Title ,,新{@class =text-danger})
< / div>
< / div>
< div class =form-group>
@ Html.LabelFor(model => model.Description,htmlAttributes:new {@class =control-label col-md-2})
< div class =col-md- 10\" >
@ Html.EditorFor(model => model.Description,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.Description ,,新{@class =text-danger})
< / div>
< / div>
< div class =form-group>
@ Html.LabelFor(model => model.PostedOn,htmlAttributes:new {@class =control-label col-md-2})
< div class =col-md- 10\" >
@ Html.EditorFor(model => model.PostedOn,new {htmlAttributes = new {@class =form-control}})
@ Html.ValidationMessageFor(model => model.PostedOn ,,新{@class =text-danger})
< / div>
< / div>
< div class =form-group>
< div class =col-md-offset-2 col-md-10>
< input type =submitvalue =创建class =btn btn-default/>
< / div>
< / div>
< / div>
}
1。确保在appSettings下的web.config中有以下行
< add key = ClientValidationEnabled value = true / >
< add key = UnobtrusiveJavaScriptEnabled value = true / >
2.确保_Layout.cshtml中有以下内容(我假设您的默认母版页),将其放在jquery下面/@Scripts.Render(\"~/bundles/jquery)。希望你有相同的设置。
@ Scripts.Render(〜/ bundles / jqueryval)
I am getting Entity Validation Error on clicking to the AddNewPost Page. usually the validation error occurs when you submit the form but when i click on the page where actually the submit form is, i am getting an entity validation error. When i debug the code to check which database entities are making problem. i saw three entities are required. but these entities should prompted such error when i submit the form. what are the possible solutions when these error occurs on going to the form page.
What I have tried:
The error I am getting
Server Error in '/' Application. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
Visual Studio Debugging Image
http://imgur.com/a/CbpEM
Controllers Index and add post
public ActionResult Index() { AdminObserver observer1 = new AdminObserver(); ActivityObserver observer2 = new ActivityObserver(); // ForumNotifier notifier = new ForumNotifier(); ForumNotfier notifier = new ForumNotfier(); notifier.Subscribe(observer1); notifier.Subscribe(observer2); return View(); //return View(); } public ActionResult AddPost(ForumPost post) { post.PostedOn = DateTime.Now; using (AppDbContext db = new AppDbContext()) { try { db.ForumPosts.Add(post); db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } ViewBag.Message = "Post submitted successfully!"; ForumNotfier notifier = new ForumNotfier(); notifier.Notify(post); return View("Index", post); }
Forumpost in model
namespace observer.Models { [Table("ForumPosts")] public class ForumPost { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] [StringLength(20)] public string UserName { get; set; } [Required] [StringLength(100)] public string Title { get; set; } [Required] [StringLength(100)] public string Description { get; set; } [Required] public DateTime PostedOn { get; set; } }
AddPost View
@using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>ForumPost</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.PostedOn, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.PostedOn, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.PostedOn, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> }
1. Make sure you have the following line in web.config under appSettings
<add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" />
2. make sure you have the following in the _Layout.cshtml (i assumed that your default master page), place it below the jQUery/@Scripts.Render("~/bundles/jquery"). Hopefully you have the same setting.
@Scripts.Render("~/bundles/jqueryval")
这篇关于ASP.NET MVC中的实体验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!