I have the following 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) {
What is my opinion:
<%@ 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 checking MVC 0 is not valid, but really for me. How can I fix this since I did not apply any validation in the Id property?
UPDATE: I donβt know how and why, but renaming Id, in my case to PK, solves this problem.
Do you know that this is a problem in my logic / configuration, or is an error or expected behavior?
asp.net-mvc-2 modelstate asp.net-mvc-2-validation
Zote
source share