本文整理汇总了C#中ModelMetadata类的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadata类的具体用法?C# ModelMetadata怎么用?C# ModelMetadata使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ModelMetadata类属于命名空间,在下文中一共展示了ModelMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ApplyMetadataAwareAttributes
private static void ApplyMetadataAwareAttributes(IEnumerable<Attribute> attributes, ModelMetadata result)
{
foreach (IMetadataAware awareAttribute in attributes.OfType<IMetadataAware>())
{
awareAttribute.OnMetadataCreated(result);
}
}
示例2: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var name = metadata.GetDisplayName();
var message = FormatErrorMessage(name);
yield return new TodayOrLaterClientValidationRule(message);
}
示例3: GetClientValidationRules
/// <summary>
/// When implemented in a class, returns client validation rules for that class.
/// </summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <returns>
/// The client validation rules for this validator.
/// </returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessageString ?? "Field invalid. Field can only contain alphanumeric characters: 'a-z', 'A-Z', '0-9'.", ValidationType = clientValidationRule };
rule.ValidationParameters.Add("pattern", RootModuleConstants.AlphanumericExpression);
yield return rule;
}
示例4: DefaultValues
public void DefaultValues()
{
// Arrange
Mock<ModelMetadataProvider> provider = new Mock<ModelMetadataProvider>();
// Act
ModelMetadata metadata = new ModelMetadata(provider.Object, typeof(Exception), () => "model", typeof(string), "propertyName");
// Assert
Assert.Equal(typeof(Exception), metadata.ContainerType);
Assert.True(metadata.ConvertEmptyStringToNull);
Assert.Null(metadata.DataTypeName);
Assert.Null(metadata.Description);
Assert.Null(metadata.DisplayFormatString);
Assert.Null(metadata.DisplayName);
Assert.Null(metadata.EditFormatString);
Assert.False(metadata.HideSurroundingHtml);
Assert.Equal("model", metadata.Model);
Assert.Equal(typeof(string), metadata.ModelType);
Assert.Null(metadata.NullDisplayText);
Assert.Equal(10000, metadata.Order);
Assert.Equal("propertyName", metadata.PropertyName);
Assert.False(metadata.IsReadOnly);
Assert.True(metadata.RequestValidationEnabled);
Assert.Null(metadata.ShortDisplayName);
Assert.True(metadata.ShowForDisplay);
Assert.True(metadata.ShowForEdit);
Assert.Null(metadata.TemplateHint);
Assert.Null(metadata.Watermark);
}
示例5: OnMetadataCreated
public virtual void OnMetadataCreated(ModelMetadata metadata)
{
string smallKey = Name.ToLowerInvariant();
string key = string.Format(CultureInfo.InvariantCulture, Prefix, smallKey);
metadata.AdditionalValues[key] = Value;
}
示例6: OnMetadataProcess
protected override void OnMetadataProcess(ModelMetadata meta, string name, ViewDataDictionary viewData, ControllerContext context)
{
base.OnMetadataProcess(meta, name, viewData, context);
if (name.IsEmpty())
{
throw new Exception("Name from ControlFor is empty");
}
this.Name = name;
if (meta.IsReadOnly)
{
this.ReadOnly = true;
}
if (this.FieldLabel.IsEmpty())
{
this.FieldLabel = meta.GetDisplayName();
}
if (this.Note.IsEmpty() && meta.Description.IsNotEmpty())
{
this.Note = meta.Description;
}
ModelState modelState;
if (viewData.ModelState.TryGetValue(name, out modelState))
{
if (modelState.Errors.Count > 0)
{
this.CustomConfig.Add(new ConfigItem("activeError", JSON.Serialize(modelState.Errors.Select(e => e.ErrorMessage)), ParameterMode.Raw));
}
}
}
示例7: ListBox
public ListBox(HtmlHelper html, string htmlFieldName, IEnumerable<SelectListItem> selectList, ModelMetadata metadata)
{
this.html = html;
this._model.htmlFieldName = htmlFieldName;
this._model.selectList = selectList;
this._model.metadata = metadata;
}
示例8: GetClientValidationRules
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule();
rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
rule.ValidationType = "dbisbool";
yield return rule;
}
示例9: MultipleAttributesCanSetValuesOnMetadata
public void MultipleAttributesCanSetValuesOnMetadata()
{
// Arrange
string name1 = "name1";
string name2 = "name2";
object value1 = new object();
object value2 = new object();
object value3 = new object();
ModelMetadata modelMetadata = new ModelMetadata(new Mock<ModelMetadataProvider>().Object, null, null, typeof(object), null);
AdditionalMetadataAttribute attr1 = new AdditionalMetadataAttribute(name1, value1);
AdditionalMetadataAttribute attr2 = new AdditionalMetadataAttribute(name2, value2);
AdditionalMetadataAttribute attr3 = new AdditionalMetadataAttribute(name1, value3);
// Act
attr1.OnMetadataCreated(modelMetadata);
attr2.OnMetadataCreated(modelMetadata);
attr3.OnMetadataCreated(modelMetadata);
// Assert
Assert.Equal(2, modelMetadata.AdditionalValues.Count);
Assert.Equal(modelMetadata.AdditionalValues[name1], value3);
Assert.Equal(modelMetadata.AdditionalValues[name2], value2);
Assert.NotEqual(attr1.TypeId, attr2.TypeId);
Assert.NotEqual(attr2.TypeId, attr3.TypeId);
Assert.NotEqual(attr3.TypeId, attr1.TypeId);
}
示例10: GetValidators
//private class LocalizableAttributeCacheKey
//{
// public LocalizableAttributeCacheKey(Attribute attribute)
// {
// string cacheFormat = "Attribute HashCode:{0}; CultureName LCID:{1}";
// this.CacheKey = string.Format(cacheFormat, attribute.GetHashCode(), System.Threading.Thread.CurrentThread.CurrentCulture.LCID);
// }
// public string CacheKey { get; private set; }
// public override int GetHashCode()
// {
// return CacheKey.GetHashCode();
// }
// public override bool Equals(object obj)
// {
// return this.GetHashCode() == ((LocalizableAttributeCacheKey)obj).GetHashCode();
// }
//}
//private static readonly List<LocalizableAttributeCacheKey> localized = new List<LocalizableAttributeCacheKey>();
//protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
//{
// foreach (ValidationAttribute attribute in attributes.OfType<ValidationAttribute>())
// {
// var cacheKey = new LocalizableAttributeCacheKey(attribute);
// if (!localized.Contains(cacheKey))
// {
// lock (localized)
// {
// if (!localized.Contains(cacheKey))
// {
// if (!string.IsNullOrWhiteSpace(attribute.ErrorMessage))
// {
// attribute.ErrorMessage = attribute.ErrorMessage.Localize();
// }
// else
// {
// if (attribute is RequiredAttribute)
// {
// attribute.ErrorMessage = "Required".Localize();
// }
// }
// localized.Add(cacheKey);
// }
// }
// }
// }
// var validators = base.GetValidators(metadata, context, attributes);
// return validators;
//}
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
foreach (var validator in base.GetValidators(metadata, context, attributes))
{
yield return new ModelValidatorWrapper(validator, metadata, context);
}
}
示例11: OnMetadataCreated
/// <summary>
/// When implemented in a class, provides metadata to the model metadata creation process.
/// </summary>
/// <param name="metadata">The model metadata.</param>
public void OnMetadataCreated(ModelMetadata metadata)
{
metadata.AdditionalValues.Add("LookupMethodName", this.LookupMethodName);
metadata.AdditionalValues.Add("AddLookupData", this.AddLookupData);
metadata.AdditionalValues.Add("LookupType",this.LookupType);
metadata.TemplateHint = "DropDownList";
}
示例12: GetClientValidationRules
/// <summary>
/// Gets the client validation rules.
/// </summary>
/// <param name="metadata">The metadata.</param>
/// <param name="context">The context.</param>
/// <returns>The client validation rules</returns>
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new[]
{
new ModelClientValidationStringLengthRule(this.FormatErrorMessage(metadata.GetDisplayName()), this.minimumCharacterCount, int.MaxValue)
};
}
示例13: GetValidators
public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
{
var validators = new List<ModelValidator>();
if (metadata is FluentModelMetadata)
{
var isPropertyMetadata = !string.IsNullOrEmpty(metadata.PropertyName);
var rules = (metadata as FluentModelMetadata).Metadata.Rules;
if (isPropertyMetadata)
{
validators.AddRange(
rules
.Select(rule => new RuleModelValidator(rule, metadata, context))
.Cast<ModelValidator>()); //TODO unnecessary for .NET 4
}
else
{
validators.AddRange(
rules
.Select(rule => new ClassRuleModelValidator(rule as IClassRule, metadata, context))
.Cast<ModelValidator>()); //TODO unnecessary for .NET 4
}
}
return validators;
}
示例14: MyValidator
public MyValidator(ValidationAttribute attribute, string errorMsg, ModelMetadata metadata, ControllerContext controllerContext, IEnumerable<ModelClientValidationRule> create)
: base(metadata, controllerContext)
{
_attribute = attribute;
_errorMsg = errorMsg;
_create = create;
}
示例15: TransformMetadata
public void TransformMetadata(ModelMetadata metadata, IEnumerable<Attribute> attributes)
{
if (!string.IsNullOrEmpty(metadata.PropertyName) && !attributes.OfType<DisplayNameAttribute>().Any())
{
metadata.DisplayName = metadata.PropertyName.ToProperCaseWords();
}
}