1

I have a Model bound by the default Model Binder. It registers a Property as invalid based on a DataAnnotations.RequiredAttribute Another property on the same model has a setter method that derives a valid value for that field and sets it.

However the original ModelState Error remains.
Main Question: Can I re-evaluate the ModelState?
Sub Question: Is there a better solution?

Edit
here's a simplified example of the model being bound (for the sub question)

public class Booking
{
    public List<Participant> Participants {get;set;}
    [Required]
    public string MainEmail {get;set;}
    private int _mainParticpantIndex;
    [Required]
    public int MainParticipantIndex
    {
        get { return _mainParticpantIndex; }
        set 
        {
            _mainParticpantIndex= value;
            MainEmail = Particpants[value].Email
        }
    }
}
public class Participant
{
    // not required for every participant.
    public string Email {get;set;}
    [Required]
    public string Name {get;set;}
}
Myster
  • 17,704
  • 13
  • 64
  • 93

1 Answers1

1

I think in this case you should remove Required from MainEmail, or maybe even don't make it settable at all. Not sure how the rest of your code works, but as I see it it will always have the value of the email of the main participant, which is also required.

Unless you want to be able to override the main email to be different than the main participant's. In which case you will probably have to modify the setter for MainParticipantIndex

Edit

Based on your comment, you can't really do what you want with the normal Required attribute. The MVC framework has no way of knowing that you want the 'requiredness' to flow through to a different object. You would have to write your own entity-level validation that would enforce your rules.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • the issue is that the Participant.Email is _not_ required. (and is not intended to be) You must select a main participant, but I wish to ensure they select someone who has an email, or add an email to the participant they do pick. (the email is duplicated as this matches the DB schema I have to work with) updated example to show Name which _is_ mandatory. – Myster Jul 21 '10 at 05:05
  • I believe the requiredness is on the correct model, the issue is that it is evaluated during modelbinding and not again after the derived value has been set in the required field – Myster Jul 21 '10 at 22:14
  • So assume the defintion of RequiredAttribute in the context of model binding is that for each property marked with that attribute a value must be provided during model binding, and not later. By that defintion your usage of the attribute is incorrect. – marcind Jul 22 '10 at 16:59
  • very true, however I considered the context of the business model. :-) Is re-evaluating the ModelState something that I should be able to do? – Myster Jul 26 '10 at 00:09
  • For others looking, I just found this, but have not tried it yet: http://stackoverflow.com/questions/2050161/validating-dataannotations-with-validator-class – Myster Jul 26 '10 at 00:09