Major Productions
Major Productions

Reputation: 6042

ASP.NET MVC 2 - Some validation errors are not appearing

I'm testing my view/edit model's Data Annotations, and some of the errors aren't showing up. They're all property-level, but they're not showing up as either property-level or model-level. They're simply not showing up at all.

My view/edit model:

public class AdminGameEditModel
{
    [Required]
    public int GameID { get; set; }

    [Required(ErrorMessage="A game must have a title")]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string GameTitle { get; set; }

    [Required(ErrorMessage="A short URL must be supplied")]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string Slug { get; set; }

    [Required(ErrorMessage="A box art image must be supplied")]
    public HttpPostedFileBase BoxArt { get; set; }

    [Required(ErrorMessage="A large image for the index page is required")]
    public HttpPostedFileBase IndexImage { get; set; }

    [Required(ErrorMessage="A game must have a review")]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string ReviewText { get; set; }

    [Required(ErrorMessage="A game must have a score")]
    public int ReviewScore { get; set; }

    [Required(ErrorMessage="A game must have at least one Pro listed")]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string[] Pros { get; set; }

    [Required(ErrorMessage="A game must have at least one Con listed")]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string[] Cons { get; set; }

    [Required(ErrorMessage="A game must belong to a genre")]
    public int GenreID { get; set; }

    [Required(ErrorMessage="A game must be associated with at least one platform")]
    public int[] PlatformIDs { get; set; }
}

The properties whose validation doesn't seem to be working properly are Pros, Cons, and GenreID. Here's how I'm trying to invoke them in my view:

<p>
    <%: Html.Label("Genre") %>
    <%: Html.ValidationMessageFor(model => Model.GameData.GenreID) %>
    <%: Html.DropDownListFor(m => Model.GameData.GenreID, new SelectList(Model.AllGenres, "GenreID", "Name", Model.GameData.GenreID)) %>
</p>

<p>
    <%: Html.LabelFor(model => Model.GameData.Pros) %><br />
    <%  for (var i = 0; i < 5; ++i)
        { %>
            <input type="text" name="GameData.Pros" value="<%: (Model.GameData.Pros[i] != null && String.IsNullOrEmpty(Model.GameData.Pros[i])) ? "" : Model.GameData.Pros[i] %>" /><br />
    <% } %>

    <%: Html.ValidationMessageFor(model => Model.GameData.Pros) %>
</p>

<p>
    <%: Html.LabelFor(model => Model.GameData.Cons) %><br />
    <%  for (var i = 0; i < 5; ++i)
        { %>
            <input type="text" name="GameData.Cons" value="<%: (Model.GameData.Cons[i] != null && String.IsNullOrEmpty(Model.GameData.Cons[i])) ? "" : Model.GameData.Cons[i] %>" /><br />
    <% } %>

    <%: Html.ValidationMessageFor(model => Model.GameData.Cons) %>
</p>

The rest all show up fine. I'm stumped as to why those three aren't appearing. I don't see anything that jumps out to me as the cause. I'm using the default model binder and validation service.

Any ideas?

Upvotes: 0

Views: 517

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

Well, for starters.. your input fields have no id's. Model validation doesn't work with names, only id's. But that's only part of the problem. The Model Binder is unlikely to be able to bind to arrays because arrays are immutable, this makes it hard to do iterative assignment to them. You're going to have to rethink this part of your application.

Second, your DropDownList has no default value. It will, in most cases just select the first item so there is no way for it to not be valid.

You may find this article interesting.

Upvotes: 1

Related Questions