ASP.MVC 2 RTM + Id 属性的 ModelState 错误
我有这个类:
public class GroupMetadata
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
[MetadataType(typeof(GrupoMetadata))]
public partial class Group
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
这个动作:
[HttpPost]
public ActionResult Edit(Group group)
{
if (ModelState.IsValid)
{
// Logic to save
return RedirectToAction("Index");
}
return View(group);
}
这是我的观点:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Group>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<fieldset>
<%= Html.EditorForModel() %>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back", "Index") %>
</div>
</asp:Content>
但是 ModelState 总是无效的!正如我所看到的,对于 MVC 验证 0 是无效的,但对我来说是有效的。 由于我没有在 Id 属性中进行任何类型的验证,我该如何修复它?
更新: 我不知道如何或为什么,但将 Id 重命名(在我的例子中为 PK)可以解决这个问题。
您知道这是我的逻辑/配置中的问题还是错误或预期行为吗?
I have this classes:
public class GroupMetadata
{
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
[MetadataType(typeof(GrupoMetadata))]
public partial class Group
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
And this action:
[HttpPost]
public ActionResult Edit(Group group)
{
if (ModelState.IsValid)
{
// Logic to save
return RedirectToAction("Index");
}
return View(group);
}
That's my view:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Group>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<fieldset>
<%= Html.EditorForModel() %>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back", "Index") %>
</div>
</asp:Content>
But ModelState is always invalid! As I can see, for MVC validation 0 is invalid, but for me is valid.
How can I fix it since, I didn't put any kind of validation in Id property?
UPDATE:
I don't know how or why, but renaming Id, in my case to PK, solves this problem.
Do you know if this an issue in my logic/configuration or is an bug or expected behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就在
if (ModelState.IsValid)
使用此行ModelState.Remove("Id")
删除 Id 索引之前,这样当 MVC 团队删除此错误时,您只需删除您项目的这一行代码。Just before
if (ModelState.IsValid)
remove Id index using this lineModelState.Remove("Id")
this way when MVC team remove this bug, you just need to remove this line code of your projects.您有一个必填字段,
Id
。它是必需的,因为它不可为空。您必须 (1) 将其与表单一起提交,或者 (2) 更改模型以使Id
可为空,或者 (3) 使用不同的类型。You have a required field,
Id
. It's required because it's non-nullable. You must either (1) submit it with your form or (2) change your model to makeId
nullable or (3) use a different type.我可以确认使用
ModelState.Remove("Id")
删除 id 索引是有效的。谁能对这个所谓的“错误”给出详细的解释?
也许 Microsoft MVC 团队的人可以给出解释?
使用 ADO.NET 实体数据模型作为数据源时不存在这样的错误 - 仅在 Linq To SQL 上。
I can confirm that removing the id index using
ModelState.Remove("Id")
works.Could anyone give an elaborate explanation for this alleged "bug"?
Maybe someone from the Microsoft MVC Team could give an explanation?
There are no bugs like this when using the ADO.NET Entity Data Model as a data source - only on Linq To SQL.
您的问题是您在模型中使用名为“id”的属性。尝试使用不同的名称,您会发现它有效。 Mvc 似乎有这个问题。
我遇到了完全相同的问题。
Your problem is that you are using a property named 'id' in your model. Try using a different name and you will see it works. Mvc seems to have a problem with that.
I experienced the exact same issue.
另一个原因可能是:
如果您将密钥声明为数字以外的某种类型,例如 String,那么由于默认情况下它无法自动为您生成密钥,因此您将面临此错误。
One other reason can be:
If you declare your key as some type other than a number e.g String, then since by default it can't auto generate the keys for you, you'll face this error.