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();
ForumNotfier notifier = new ForumNotfier();
notifier.Subscribe(observer1);
notifier.Subscribe(observer2);
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>
}