Im new to asp.net MVC4 , I have an MVC4 application that is using SQL Server 2012 EntityFramework (code first). The Edit post method is not saving the data. On checking if ModelState.IsValid it return false, can anyone help me find whats wrong with my code
MODEL
public class Customer
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="*")]
public string FirstName { get; set; }
[Required(ErrorMessage = "*")]
public string LastName { get; set; }
[Required(ErrorMessage = "*")]
[MaxLength(1, ErrorMessage="Initial only")]
public string MI { get; set; }
[Required(ErrorMessage = "*")]
public string Address { get; set; }
[Required(ErrorMessage = "*")]
public String ContactNo { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email")]
public string EmailAddress { get; set; }
[Required(ErrorMessage = "*")]
[MaxLength(8, ErrorMessage = "Max of 8")]
[MinLength(5, ErrorMessage = "Min of 5")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[NotMapped]
public string Password { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Password)]
[NotMapped]
[Display(Name = "Retype-PW")]
public string RetypePassword { get; set; }
[Required]
[Display(Name = "How much is")]
[NotMapped]
public string Captcha { get; set; }
}
EDIT VIEW
<h2>Edit CUSTOMER</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Customer</legend>
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model=>model.Captcha)
@Html.HiddenFor(model=>model.Password)
@Html.HiddenFor(model=>model.RetypePassword)
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MI)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MI)
@Html.ValidationMessageFor(model => model.MI)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ContactNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ContactNo)
@Html.ValidationMessageFor(model => model.ContactNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
EDIT CONTROLLER METHOD
public ActionResult Edit(int id = 0)
{
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
//
// POST: /Customer/Edit/5
[HttpPost]
public ActionResult Edit(Customer customer)
{
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
if (User.IsInRole("Administrator"))
{
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Details");
}
}
return View(customer);
}
Thanks for the help