I have two structurally identical tables that each have simple Create New, and Update forms. One of the tables works perfect when both updating and adding new rows... The other table works fine f开发者_如何学运维or updating, but can't make it past ModelState.IsValid when creating a new row.
I'm given an error message that says the 'ID' field must be included even though that is an auto-incrementing primary key. I also tried submitting a null value but that doesn't work either.
As far as I can tell both of my primary keys are set up the same way and have they both use a LINQ to Entities Model.
[HttpPost]
public ActionResult CreateProduct(Product product)
{
if (ModelState.IsValid)
{
productrepository.Add(product);
productrepository.Save();
return RedirectToAction("ProductList");
}
return View();
}
Any suggestions?
Thanks!
Exclude the ID with the Bind Attribute:
[HttpPost]
public ActionResult Create([Bind(Exclude="ID")]CompanyAddress companyAddress)
{
if (ModelState.IsValid)
I changed my auto-incrementing column name from 'ID' to 'AutoID' and now it seems to work.
This was the only difference I could find between the two tables so I'm guessing either my model needed to be updated or using 'ID' was implying something unintentional.
I'm still not sure why it didn't work, I'm just glad I got it working!
精彩评论