Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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>
}
Posted
Updated 20-Feb-17 14:55pm

1 solution

1. Make sure you have the following line in web.config under appSettings
HTML
<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.

HTML
@Scripts.Render("~/bundles/jqueryval")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900