1

I have a requirement where I need to validate the dropdown. On a Button1 click the Model should validate that the dropdown is not selected and on Button2 click the Model should validate that the dropdown is selected to a valid value and also a corresponding field is also marked as Required if the Value is a certain value from the dropdown.

My model is as below:

public class ApprovalInformation
{

    [DisplayName("Approval Decision")]
    public int? ApproveStatusID { get; set; }
    public string ApproveStatus { get; set; }
    public SelectList ApproveStatuses { get; set; }

    [DisplayName("Additional Information")]
    [RequiredIf("ApproveStatus", StringConstants.NotApproved, ErrorMessage = "You must specify the comments if not approved")]
    public string AdditionalInformation { get; set; }
}

Currently I have 2 action methods and I call them based on the value of the button name. Here is the controller code:

public ActionResult SaveApproval(ApprovalInformation approveInfo,string submit)
    {
        if (submit == "Save For Later")
        {
            Business business = new Business();
            int selectedStatusID = approveInfo.ApproveStatusID??0;
            if ( selectedStatusID!= 0)
            {
                ModelState.AddModelError("ApproveStatusID", "You may not set the Approval Decision before saving a service request for later.  Please reset the Approval Decision to blank");
            }
            if (ModelState.IsValid)
            {
              return RedirectToActionPermanent("EditApproval");
            }

            return View("EditApproval", approveInfo);
        }
        else
        {
            TempData["approverInfo"] = approveInfo;
            return RedirectToActionPermanent("FinishApproval");
        }
    }

I am having a problem plugging the validation depending on the buttons clicked. Since on different button click the same property should be validated in 2 different manner. How can i suppress a Validation or Induce validations at run time on the same model depending on different actions. Any idea around this will be appreciated.

Nisha_Roy
  • 424
  • 2
  • 9
  • 15

1 Answers1

1

I believe this is a good situation to implement the IValidatableObject Interface on your ApprovalInformation view model. You can pass through the intent to submit or save for later in the ValidationContext dictionary, in order to get the reuse you need.

You can also place the conditional logic that "AdditionalInformation must be set if ApprovalStatus is not set" here, as well.

public class ApprovalInformation : IValidatableObject
{
    ... // Properties

    IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
       if (validationContext.ContainsKey("submit"))
       {
          if (ApproveStatusID != 0)
          {
              yield return new ValidationResult("You may not set the Approval Decision before saving a service request for later.  Please reset the Approval Decision to blank", 
                                                 new {"ApproveStatusID"});
          }
       }
    }
}
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285