ASP.NET MVC 2 - Reevaluate ModelState
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 b开发者_StackOverflow中文版eing 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;}
}
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.
精彩评论