由于不包含自动增量字段,ModelState.IsValid 返回 FALSE
我有两个结构相同的表,每个表都有简单的“新建”和“更新”表单。其中一个表在更新和添加新行时工作得很好...另一个表在更新时工作得很好,但在创建新行时无法通过 ModelState.IsValid 。
我收到一条错误消息,指出必须包含“ID”字段,即使这是一个自动递增主键。我也尝试提交空值,但这也不起作用。
据我所知,我的两个主键的设置方式相同,并且它们都使用 LINQ to Entities 模型。
[HttpPost]
public ActionResult CreateProduct(Product product)
{
if (ModelState.IsValid)
{
productrepository.Add(product);
productrepository.Save();
return RedirectToAction("ProductList");
}
return View();
}
有什么建议吗?
谢谢!
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 for 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用绑定属性排除 ID:
Exclude the ID with the Bind Attribute:
我将自动递增列名称从“ID”更改为“AutoID”,现在它似乎可以工作。
这是我能在两个表之间找到的唯一区别,因此我猜测要么我的模型需要更新,要么使用“ID”暗示了一些无意的事情。
我仍然不确定为什么它不起作用,我只是很高兴我让它起作用了!
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!