Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using custom fluent validation and modal popup having controls. Only the required field validator is displaying error message on submit. How to return custom error message on to the same modal popup.

Model
VB
RuleFor(x => x.Participants)
                .NotEmpty()
                .WithMessage("Please Select Participant.")
                .Must(IsChaimanPresentValidator)
                .WithMessage("Director type with 'Chairman' is required in Participant");



C#
private bool IsChaimanPresentValidator(string directorTypeList)
        {
            if (directorTypeList != null)
            {
                string[] directorTypes = directorTypeList.Split(',');

                string directorType = (DirectorType.Chairman).ToString();
                if (directorTypes.Contains(directorType))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }


Controller
public ActionResult Create()
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageMeeting))
return AccessDeniedPublicView();

MeetingModel objMeetingModel = new MeetingModel();
try
{
objMeetingModel.Id = 0;
objMeetingModel.CompanyList = _companyService.GetAllCompanies().Select(x => x.ToModel()).ToList();
objMeetingModel.MeetingTypeList = getMeetingType();
objMeetingModel.MeetingStatusList = getMeetingStatus();
}
catch (Exception ex)
{
LogException(ex);
}
ViewBag.IsEditPage = false;
return PartialView("_Create",objMeetingModel);
}

C#
[HttpPost]
        public ActionResult Create(MeetingModel meetingmodel)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageMeeting))
                return AccessDeniedPublicView();
            try
            {              
                //check if model state is valid
                if (!ModelState.IsValid)
                {

                    var allErrors = ModelState.Values.SelectMany(v => v.Errors);                                       
                    return Json(new { success = false, Errors = ModelState.SerializeErrors() });
               }

}
Posted
Comments
Afzaal Ahmad Zeeshan 10-Nov-15 1:50am    
How do you want to return the error?

Why can't you simply throw new CustomException();?

1 solution

as unobstructive error in modal popup.
I tried to return PartialView("_Create", meetingmodel); but view is getting opened on new window without any bootstrap design.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900