public static List <SelectListItem> GetSourceForComboBox( PropertyMetaData property, object currentValue, IEnumerable source) { var result = GetSelectedListItems(property, source, currentValue); if (LinqToSqlUtils.CanBeNull(property.Info)) { result.Insert(0, _emtpySelectItem); } return(result); }
public static bool Validate(ModelStateDictionary modelState, object obj) { var type = obj.GetType(); foreach (var propertyInfo in type.GetProperties()) { var metaData = MvcApplication.MetaDataProvider.Get(type); var value = propertyInfo.GetValue(obj); var canBeNull = LinqToSqlUtils.CanBeNull(propertyInfo); var isDefault = false; var isForeign = LinqToSqlUtils.GetForeignProperty(type, propertyInfo.Name) != null; if (value != null) { isDefault = Equals(value.GetType().Default(), value); } if (!canBeNull && (value == null || Equals(value, string.Empty) || (isForeign && isDefault))) { var displayName = metaData.GetProperties().First(x => x.Info == propertyInfo).DisplayName(); modelState.AddModelError(propertyInfo.Name, ValidationMessages.notempty_error.Replace( ValidationMessages.PropertyName, displayName)); } /* if(propertyInfo.Info.PropertyType == typeof(string)) * { * var strValue = (string)value; * if (strValue != null) * { * var maxLength = LinqToSqlUtil.GetMaxLength( * propertyInfo.Info); * if (maxLength > 0 && strValue.Length > maxLength) * modelState.AddModelError(propertyInfo.Info.Name, "Поле <" + * propertyInfo.DisplayName + "> не должно быть длиннее " + * maxLength); * } * * }*/ } return(modelState.IsValid); }
public static MvcHtmlString DropDownListFor <Tm, Tp, Ti>(this HtmlHelper <Tm> htmlHelper, Expression <Func <Tm, Tp> > expression, IEnumerable <Ti> source, Func <Ti, object> textSelector, Func <Ti, object> valueSelector, IDictionary <string, object> htmlAttributes) where Tm : class { var inputName = ExpressionHelper.GetInputName(expression); // var local = GetValue(htmlHelper, expression); var selectItemList = new List <SelectListItem>(); var propertyInfo = ((MemberExpression)(expression.Body)).Member as PropertyInfo; if (LinqToSqlUtils.CanBeNull(propertyInfo)) { selectItemList.Add( new SelectListItem { Text = "Нет", Value = string.Empty, }); } selectItemList.AddRange(SelectListUtil.GetSelectItemList(source, textSelector, valueSelector)); return(htmlHelper.DropDownList(inputName, selectItemList, htmlAttributes)); }