-1

MVC 5 app....

I have a model with many fields... 2 of them are... (This is the model.)

public string eventStatus {get; set;}
public DateTime? eventDateStarted {get; set;}
public DateTime? eventDateCompleted {get; set;}

What I want to do is never allow eventStatus to be set to "Closed" UNLESS the eventDateStarted and eventDateCompleted fields are not null. If so then result in an invalid ModelState.

PLEASE NOTE: There is programming logic involved here, which could get more advanced based on business rules. So, it's not just simply a matter of saying this field is required in order for this one to be set, etc...

I was thinking that the best place to put this logic is in the set method of eventStatus. Is this a best practice solution? If so, how can I force the ModelState to be invalid?

This is what I've written so far...

    [Display(Name = "Status")]
    public string eventStatus
    {
        set
        {
            if (EventActualEnd is null || EventActualStart is null)
            {
                ModelState.AddModelError("Status", "Start and End date is mandatory");
            } 
        }
    }

But, it doesn't like AddModelError.

WebDevGuy2
  • 1,159
  • 1
  • 19
  • 40
  • Possible duplicate of [attribute dependent on another field](http://stackoverflow.com/questions/3713281/attribute-dependent-on-another-field) – Cristian Szpisjak Apr 12 '17 at 16:26
  • Create a conditional validation attribute. For a good guide, refer [The Complete Guide To Validation In ASP.NET MVC 3 - Part 2](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Apr 13 '17 at 11:08

1 Answers1

1

I'm not sure where your code for AuditStatusId is sitting, but it looks to be within a model to me. ModelState is a property of the controller and can only be accessed from classes that inherit from System.Web.Mvc.Controller.

Are external libraries a viable option for you? If so I would check out MVC Foolproof which is available through NuGet. It allows complex model data annotations which are built in that would accomplish exactly what you would want. You can set custom validations based on many different situations and create your own.

If that's not a possibility, by default MVC is somewhat limited on built in data annotations on the model for validation. You would have to extend with your own, or in the controller logic where you normally check server side for the ModelState.IsValid, add in your own logic for checking the current model variables and either continuing on or breaking out of the logical path, basically doing a ModelState.IsValid by hand. With a check that isn't very complex like this, it should be very little code, and if you need to do it multiple places you could break it out into a utility class and pass in the model, do the validation there, and return a true or false for validated. Again this would be server side only, you would need to do some similar magic on the client end if you wanted client side validation based on how your application is built.

Nard Dog
  • 906
  • 1
  • 18
  • 33
  • thanks. Yes, that was code in the model (set method). I edited it above. This is my own app so I can possibly use Foolproof advanced annotation. Thanks for your suggestions. – WebDevGuy2 Apr 12 '17 at 16:47