예제 #1
0
        public static MvcHtmlString AppCheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, object htmlAttributes)
        {
            var control = new RenderCheckBox();

            control.Name = name;
            if (isChecked)
            {
                control.Checked = isChecked.ToString();
            }


            HtmlAttributes(control, htmlAttributes);

            ModelError modelError = htmlHelper.ViewData.ModelState.Where(w => w.Key == name).SelectMany(m => m.Value.Errors).FirstOrDefault();

            if (modelError != null)
            {
                control.HtmlValidateString = MvcHtmlString.Create("<span class='validation_wrapper customValidation'><span>" + modelError.ErrorMessage + "</span></span>");
            }

            return(MvcHtmlString.Create(control.ToString()));
        }
예제 #2
0
        public static MvcHtmlString AppCheckBoxFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, object htmlAttributes)
        {
            var           control  = new RenderCheckBox();
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            control.Name  = metadata.PropertyName;
            control.ID    = metadata.PropertyName;
            control.Value = true;

            Object value = metadata.Model;

            if (value != null && (bool)value == true)
            {
                control.Checked = "true";
            }

            if (!string.IsNullOrEmpty(metadata.DisplayName))
            {
                control.Text = metadata.DisplayName;
            }
            else
            {
                control.Text = metadata.PropertyName.Replace("_", " ");
            }

            HtmlAttributes(control, htmlAttributes);

            ModelError modelError = htmlHelper.ViewData.ModelState.Where(w => w.Key == metadata.PropertyName).SelectMany(m => m.Value.Errors).FirstOrDefault();

            if (modelError != null)
            {
                control.HtmlValidateString = MvcHtmlString.Create("<span class='validation_wrapper customValidation'><span>" + modelError.ErrorMessage + "</span></span>");
            }

            return(MvcHtmlString.Create(control.ToString()));
        }