예제 #1
0
        public static MvcHtmlString ValFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string validationMessage, IDictionary <string, object> htmlAttributes)
        {
            ModelMetadata metadata    = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary <TModel>());
            var           name        = ExpressionHelper.GetExpressionText(expression);
            var           validations = ModelValidatorProviders.Providers.GetValidators(
                metadata ?? ModelMetadata.FromStringExpression(name, new ViewDataDictionary()),
                new ControllerContext())
                                        .SelectMany(v => v.GetClientValidationRules()).ToArray();

            List <ValidatorMessage> validatorMessages = new List <ValidatorMessage>();
            List <ValidatorMessage> listQ             = new List <ValidatorMessage>();

            foreach (var validation in validations)
            {
                var dictionaryType = DictionaryValidationType.Where(x => x.Key == validation.ValidationType).ToArray();

                if (dictionaryType.Any())
                {
                    validatorMessages.AddRange(dictionaryType.Select(type => new ValidatorMessage()
                    {
                        Type    = type.Value,
                        Message = validation.ErrorMessage.Replace("'", "")
                    }));
                }
                else
                {
                    ValidatorMessage validatorMessage = new ValidatorMessage()
                    {
                        Type    = validation.ValidationType,
                        Message = validation.ErrorMessage.Replace("'", "")
                    };

                    validatorMessages.Add(validatorMessage);
                }
            }

            Console.WriteLine(listQ);

            string result = "";

            //result += GetNgValDirectiveString(validatorMessages);
            result += GetValidatorDirectivesString(validations);

            return(MvcHtmlString.Create(result));
            //return new MvcHtmlString(result);
        }
예제 #2
0
        public static MvcHtmlString NgValFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, string validationMessage, IDictionary <string, object> htmlAttributes)
        {
            ModelMetadata metadata    = ModelMetadata.FromLambdaExpression(expression, new ViewDataDictionary <TModel>());
            var           name        = ExpressionHelper.GetExpressionText(expression);
            var           validations = ModelValidatorProviders.Providers.GetValidators(
                metadata ?? ModelMetadata.FromStringExpression(name, new ViewDataDictionary()),
                new ControllerContext())
                                        .SelectMany(v => v.GetClientValidationRules()).ToArray();

            List <ValidatorMessage> validatorMessages = new List <ValidatorMessage>();
            ValidatorMessage        vM = new ValidatorMessage();

            foreach (var validation in validations)
            {
                var dictionaryType = DictionaryValidationType.Where(x => x.Key == validation.ValidationType).ToArray();

                if (dictionaryType.Any())
                {
                    validatorMessages.AddRange(dictionaryType.Select(type => new ValidatorMessage()
                    {
                        Type    = type.Value,
                        Message = validation.ErrorMessage.Replace("'", "")
                    }));
                }
                else
                {
                    ValidatorMessage validatorMessage = new ValidatorMessage()
                    {
                        Type    = validation.ValidationType,
                        Message = validation.ErrorMessage.Replace("'", "")
                    };

                    validatorMessages.Add(validatorMessage);
                }
            }
            //TagBuilder tag = new TagBuilder("ngVal");
            //tag.Attributes.Add("validation", htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(name));

            //if (validations.Any())
            //{
            //    foreach (var val in validations)
            //    {
            //        string valResult = GetNgValDirectiveString(validatorMessages);
            //        tag.Attributes.Add("kendo-tooltip k-content", "'" + valResult + "'");
            //    }
            //};

            //var validatorMessages = validations.ToDictionary(k => k.ValidationType, v => v.ErrorMessage);

            string result = "";

            result += GetNgValDirectiveString(validatorMessages);
            result += GetValidatorDirectivesString(validations);

            //string validatorMessagesStr = "{";
            //foreach (var validatorMessage in validatorMessages)
            //{
            //    validatorMessagesStr += validatorMessage.Key + ":'" + validatorMessage.Value + "',";
            //}
            //validatorMessagesStr += "}";

            return(new MvcHtmlString(result));
        }