I have been trying to complete a simple application I was given for practice at my summer internship. I have almost no background in ASP.NET so I was given extra work. I am trying to get a database entry to update from an http post function in c#, however when the function runs it throws a validation error. I have been trying to find reasons for the error, however I cannot seem to figure out why it is not working. Any help is appreciated.
Here is the error:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Source Error:
Line 130: db.Entry(productNote).State = EntityState.Modified;
Line 131: db.SaveChanges();
Line 132: return RedirectToAction("Index");
Source File: C:\Users\Morph\Documents\Visual Studio 2015\Projects\NotesWebApp\NotesWebApp\Controllers\ProductsController.cs Line: 131
Here is the http post function in my Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index([Bind(Include = "ID,ProductID,NoteText,CreateDate,Archived")] ProductNote productNote)
{
Response.Write("<script type=\"text/javascript\">alert('Works');</script>");
if (ModelState.IsValid)
{
db.Entry(productNote).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
Here is the form I am submitting to the function:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(noteItem => note.ID)
@Html.HiddenFor(noteItem => note.ProductID)
@Html.HiddenFor(noteItem => note.NoteText)
@Html.HiddenFor(noteItem => note.CreateDate)
<div class="checkbox">
@Html.EditorFor(noteItem => note.Archived)
@Html.ValidationMessageFor(noteItem => note.Archived, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
public ActionResult Archive(int ID)
(and you could do that using ajax so that the user can continue to archive notes while staying on the same page) – user3559349