Switching over placeholder attributes to use DataAnnotation attributes

This commit is contained in:
Pranav K 2014-03-13 15:44:12 -07:00
parent 3461c44a5e
commit d86298ffe1
7 changed files with 3 additions and 94 deletions

View File

@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Microsoft.AspNet.Mvc.ModelBinding
@ -10,20 +10,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
public CachedDataAnnotationsMetadataAttributes(IEnumerable<Attribute> attributes)
{
Display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
DisplayName = attributes.OfType<DisplayNameAttribute>().FirstOrDefault();
DisplayFormat = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
Editable = attributes.OfType<EditableAttribute>().FirstOrDefault();
ReadOnly = attributes.OfType<ReadOnlyAttribute>().FirstOrDefault();
}
public DisplayAttribute Display { get; protected set; }
public DisplayNameAttribute DisplayName { get; protected set; }
public DisplayFormatAttribute DisplayFormat { get; protected set; }
public EditableAttribute Editable { get; protected set; }
public ReadOnlyAttribute ReadOnly { get; protected set; }
}
}

View File

@ -45,44 +45,22 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
return !PrototypeCache.Editable.AllowEdit;
}
if (PrototypeCache.ReadOnly != null)
{
return PrototypeCache.ReadOnly.IsReadOnly;
}
return base.ComputeIsReadOnly();
}
public override string GetDisplayName()
{
// DisplayName could be provided by either the DisplayAttribute, or DisplayNameAttribute. If neither of
// those supply a name, then we fall back to the property name (in base.GetDisplayName()).
//
// DisplayName has lower precedence than Display.Name, for consistency with MVC.
// DisplayAttribute doesn't require you to set a name, so this could be null.
if (PrototypeCache.Display != null)
{
string name = PrototypeCache.Display.GetName();
var name = PrototypeCache.Display.GetName();
if (name != null)
{
return name;
}
}
// It's also possible for DisplayNameAttribute to be used without setting a name. If a user does that, then DisplayName will
// return the empty string - but for consistency with MVC we allow it. We do fallback to the property name in the (unlikely)
// scenario that the user sets null as the DisplayName, again, for consistency with MVC.
if (PrototypeCache.DisplayName != null)
{
string name = PrototypeCache.DisplayName.DisplayName;
if (name != null)
{
return name;
}
}
// If neither attribute specifies a name, we'll fall back to the property name.
// If DisplayAttribute does not specify a name, we'll fall back to the property name.
return base.GetDisplayName();
}
}

View File

@ -1,20 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class DisplayAttribute : Attribute
{
internal string GetDescription()
{
throw new NotImplementedException();
}
internal string GetName()
{
throw new NotImplementedException();
}
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class DisplayFormatAttribute : Attribute
{
public bool ConvertEmptyStringToNull { get; set; }
}
}

View File

@ -1,9 +0,0 @@
using System;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class DisplayNameAttribute : Attribute
{
public virtual string DisplayName { get; set; }
}
}

View File

@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class EditableAttribute : Attribute
{
public bool AllowEdit { get; set; }
}
}

View File

@ -1,10 +0,0 @@
#if K10
namespace Microsoft.AspNet.Mvc.ModelBinding
{
public class ReadOnlyAttribute
{
public bool IsReadOnly { get; set; }
}
}
#endif