ASP.MVC 2 RTM + ModelState Error at Id property
Asked Answered
S

5

7

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?

Suitcase answered 31/3, 2010 at 18:56 Comment(3)
Are the view's using the model? Is the field called "Name"? :)Flaviaflavian
@Craig, just "The Id field is required."Suitcase
@Flaviaflavian Yes view is using model and I have 2 fields: Id and Name.Suitcase
S
9

Just before if (ModelState.IsValid) remove Id index using this line ModelState.Remove("Id") this way when MVC team remove this bug, you just need to remove this line code of your projects.

Suitcase answered 31/5, 2010 at 11:34 Comment(3)
Just to say, 3 years down the line this is still a problem with MVC4... Thanks for the solutionEsmaria
Still having this issue when using MVC 5, Database First and Entity Framework 6. Having only when using ViewModels though. Thanks a lot! Your solution still works =-)Tollgate
Just though i would add. If you have a model in the viewmodel that is causing the issue you need to prefix the id. ModelState.Remove("model.ModelId");Jasonjasper
M
1

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 make Id nullable or (3) use a different type.

Moquette answered 31/3, 2010 at 20:57 Comment(2)
@Craig (1) It can't be nullble since it's my pk for Entity. (2) I'm submitting it from my form with 0 (zero) value. (3) Use another type isn't an option. And you are right, why it solves the error when I change name form Id to PK ? RegardsSuitcase
You say you're submitting it, but the error message says you're not. That, plus the fact that it works when you rename it, probably means you got the name wrong. Check the form data with Fiddler or Firebug to be sure.Moquette
G
1

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.

Gaultheria answered 16/11, 2010 at 10:30 Comment(0)
D
0

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.

Deathly answered 28/5, 2010 at 18:44 Comment(1)
Thank you I tried your way, but for my project I got a "cleaner" option. See my answer here.Suitcase
D
0

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.

Dicotyledon answered 3/8, 2012 at 5:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.