如何将custum验证错误消息返回到mvc中的模态弹出窗口 [英] How to return custum validation error message to modal popup in mvc

查看:131
本文介绍了如何将custum验证错误消息返回到mvc中的模态弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义流畅验证和具有控件的模态弹出窗口。只有必填字段验证程序在提交时显示错误消息。如何将自定义错误消息返回到相同的模态弹出窗口。



模型

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

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










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;
        }





控制器

公共ActionResult创建()

{

if(!_permissionService.Authorize(StandardPermissionProvider.ManageMeeting))

返回AccessDeniedPublicView();



MeetingModel objMeetingModel = new MeetingModel();

尝试

{

objMeetingModel.Id = 0;

objMeetingModel。 。CompanyList = _companyService.GetAllCompanies()选择(X => x.ToModel())ToList();

objMeetingModel.MeetingTypeList = getMeetingType();

objMeetingModel .MeetingStatusList = getMeetingStatus();

}

catch(exception ex)

{

LogException(ex);

}

ViewBag.IsEditPage = false;

返回PartialView(_ Create,objMeetingModel);

}





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);
}

[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() });
               }



}


}

推荐答案

作为模态弹出窗口中的无阻碍错误。

我试图返回PartialView(_ Create,meetingmodel);但是在没有任何引导程序设计的情况下,视图将在新窗口中打开。
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.


这篇关于如何将custum验证错误消息返回到mvc中的模态弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆