3

I have been getting problems like these for a while but once and for all would like to know what's going on :)

I have a simple ASP.NET MVC view which is bound to a view model class MemberViewModel.

MemberViewModel contains a Linq To Sql entity object that my form is primarily bound to named Member, however I do have about three other form fields bound to a child class named Member.User.

Member contains personal info about the user, and Member.User contains Username + Password information, both of which are stored in separate tables in the DB.

Now as I stated the view's model object uses a custom view-model class entitled MemberViewModel, the contents of which are as follows:

[Bind(Exclude = "EncryptedPassword")]
public class MemberViewModel : ViewModel
{
    public Member Member { get; set; }

    public string Password { get; set; }
    [DisplayName("Confirm Password")]
    public string ConfirmPassword { get; set; }

    public MemberViewModel() { }
    public MemberViewModel(Member member, SelectList countryList)
    {
        Member = member;
        CountryList = countryList;
    }
}

You can see how there is only a single reference to Member. Member is a Linq to Sql object and inside it has it's reference to User. Password + ConfirmPassword and form only fields and do not have an equivalent counterpart in Linq To Sql.

Now my problem is, whenever I submit the form my ModelState.IsValid property always returns false stating the model error being The EncryptedPassword field is invalid.

Now regardless of whether I add [Bind(Exclude = "Member.User.EncryptedPassword")] to my MemberViewModel as a class attribute, or on the partial class for User itself as [Bind(Exclude = "EncryptedPassword")] the ModelState.IsValid continually states it is invalid.

  1. How can I get this to function and exclude child properties from model state validation?
  2. How does Bind Exclude truly work and what is the best practice for child objects and working with model binding in conjunction with view models?

Kindest Regards, GONeale

2
  • What data type is User.EncryptedPassword in your LINQ to SQL entitiy? Commented Dec 7, 2010 at 2:15
  • string (SO filler chars. here)
    – GONeale
    Commented Dec 7, 2010 at 2:53

2 Answers 2

5

The [Bind] attribute only affects model binding. That is, whether or not the ASP.NET MVC framework will try and populate the property from the request.

Your issue is related to validation - a whole other beast. :)

1
  • Thanks, couldn't believe this! I realised I have an inherited AssociatedValidatorProvider which automatically adds field validation extracted from Linq to Sql schema ruling. While quite cool, it added validation to EVERYTHING, including EncryptedPassword which obviously I don't want to be featured on the form. Here I am trying to fix it with Bind(Exclude) which as you state doesn't affect validation at all - silly me! I have gotten around these one off fields by using a ModelState.Remove(). Thanks for the quick answer!
    – GONeale
    Commented Dec 7, 2010 at 3:52
0

Though not a direct answer to your question, I can offer a little advice: try and use DTOs for passing data between the controller and the view.

You'll find that a lot of third party components and libraries make this assumption, so it will make your life easier in the long run.

By DTOs I'm referring to simple - usually flat - objects, which look something like this:

public class Dto
{
    public string PropertyName { get; set; }
    public string PropertyNameSubPropertyName { get; set; }
}

For more information, and for a tool to help with the mapping between dtos & domain objects, check out Automapper..

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.