views:

561

answers:

2

I'm using DataAnnotations attributes to validate my model objects. My model class looks similar to this:

public class MyModel
{
    [Required]
    public string Title { get; set; }

    [Required(ErrorMessage = "At least one editor is required.")]
    public List<User> Editors { get; set; }
}

public class User
{
    public int Id { get; set; }

    [Required]
    public string FullName { get; set; }

    [Required]
    [DataType(DataType.Email)]
    public string Email { get; set; }
}

My controller action looks like:

public ActionResult NewItem(MyModel data)
{
    if (!this.Model.IsValid)
    {
        // invalid processing
    }
    // valid processing
}

User is presented with a view that has a form with:

  • a text box with dummy name where users enter user's names. For each user they enter, there's a client script coupled with ajax that creates an <input type="hidden" name="Editors[0].Id" value="userId" /> for each user entered (enumeration index is therefore not always 0 as written here), so default model binder is able to consume and bind the form without any problems.
  • a text box where users enter the title

Since I'm using Asp.net MVC 2 RTM which does model validation instead of input validation I don't know how to avoid validation errors. And since users provide User.Id, the whole User object instance is being validated. Which doesn't bother me, as long I would know how to exclude other properties' validation.

The thing is I have to use BindAttribute on my controller action. I would have to either provide a white or a black list of properties. It's always a better practice to provide a white list. It's also more future proof.

The problem

My form works fine, but I get validation errors about user's FullName and Email properties since they are not provided. I also shouldn't feed them to the client (via ajax when user enters user data), because email is personal contact data and is not shared between users.

If there was just a single user reference on MyModel I would write

[Bind(Include = "Title, Editor.Id")]

But I have an enumeration of them. How do I provide Bind white list to work with my model?

One possible solution

I could create a separate view model for User just for the sake of entering MyModel objects. I would put actual validation attributes and omit properties I don't need.

public class MyModelUser
{
    [Required]
    public int Id { get; set; }
}
+1  A: 

I ended up using a separate view model (as described in One possible solution in my question), that can be fully validated. It's a workaround and I would still like to know how to do it properly.

Robert Koritnik
A: 

hi Robert!

i have the same problem here. Can you tell us how you've made your Views and Controllers to accept a collection of items and validate them?

thank you very much!

Alexandre Jobin
I've answered my own question. I created a separate view model just for the sake of making this validation work. But if you're having problems binding collection data in your controller action you can check my other question related to this: http://stackoverflow.com/questions/2400391 (and if it's helpful you can upvote my answer (and/or question as well) ;)
Robert Koritnik