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.