Search code examples
jqueryasp.net-mvcbootstrapvalidator

how to pass 'true'/'false' data from Controller to View using jquery for bootstrap validator in MVC


VIEW

$('#frmCreateNewAdminPanelMenu').bootstrapValidator({
                message: '.......',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    txtMenuName_Create: {
                        validators: {
                            notEmpty: {
                                message: '......'
                            },
                            stringLength: {
                                min: 6,
                                max: 100,
                                message: '.......'
                            },
                            remote: {
                                type: 'POST',
                                url: '/AdminPanelMenu/MenuNameRemote',
                                message: 'Menu name is not available.',
                                delay: 1000
                            }
                        }
                    }
             }
      });

At this bootstrapvalidator, txtMenuName_Create is passed for checking whether there is or not.

CONTROLLER

        public JsonResult MenuNameRemote(string txtMenuName_Create)
    {
        List<AdminPanelMenu> adminMenus = _adminPanelMenuRepo.GetAll().ToList();

        return Json(adminMenus.Any(x => x.Name.ToLowerInvariant().Trim() == txtMenuName_Create.ToLowerInvariant().Trim()), JsonRequestBehavior.AllowGet);
    }

At this controller, after txtMenuName_Create is checked, true or false data is returned. But, i don't know how to pass this data from controller to view.


Solution

  • I finally solved this.

            public JsonResult MenuNameRemote(string txtMenuNameRemote)
        {
            List<AdminPanelMenu> adminMenus = _adminPanelMenuRepo.GetAll().ToList();
    
            if (adminMenus.Any(x => x.Name.ToLowerInvariant().Trim() == txtMenuNameRemote.ToLowerInvariant().Trim()))
                return Json(new { valid = false });
    
            else return Json(new { valid = true });
        }