Switching over placeholder attributes to use DataAnnotation attributes
This commit is contained in:
parent
3461c44a5e
commit
d86298ffe1
|
|
@ -1,6 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
|
|
@ -10,20 +10,14 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
public CachedDataAnnotationsMetadataAttributes(IEnumerable<Attribute> attributes)
|
public CachedDataAnnotationsMetadataAttributes(IEnumerable<Attribute> attributes)
|
||||||
{
|
{
|
||||||
Display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
|
Display = attributes.OfType<DisplayAttribute>().FirstOrDefault();
|
||||||
DisplayName = attributes.OfType<DisplayNameAttribute>().FirstOrDefault();
|
|
||||||
DisplayFormat = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
|
DisplayFormat = attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
|
||||||
Editable = attributes.OfType<EditableAttribute>().FirstOrDefault();
|
Editable = attributes.OfType<EditableAttribute>().FirstOrDefault();
|
||||||
ReadOnly = attributes.OfType<ReadOnlyAttribute>().FirstOrDefault();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DisplayAttribute Display { get; protected set; }
|
public DisplayAttribute Display { get; protected set; }
|
||||||
|
|
||||||
public DisplayNameAttribute DisplayName { get; protected set; }
|
|
||||||
|
|
||||||
public DisplayFormatAttribute DisplayFormat { get; protected set; }
|
public DisplayFormatAttribute DisplayFormat { get; protected set; }
|
||||||
|
|
||||||
public EditableAttribute Editable { get; protected set; }
|
public EditableAttribute Editable { get; protected set; }
|
||||||
|
|
||||||
public ReadOnlyAttribute ReadOnly { get; protected set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,44 +45,22 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
|
||||||
return !PrototypeCache.Editable.AllowEdit;
|
return !PrototypeCache.Editable.AllowEdit;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PrototypeCache.ReadOnly != null)
|
|
||||||
{
|
|
||||||
return PrototypeCache.ReadOnly.IsReadOnly;
|
|
||||||
}
|
|
||||||
|
|
||||||
return base.ComputeIsReadOnly();
|
return base.ComputeIsReadOnly();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string GetDisplayName()
|
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.
|
// DisplayAttribute doesn't require you to set a name, so this could be null.
|
||||||
if (PrototypeCache.Display != null)
|
if (PrototypeCache.Display != null)
|
||||||
{
|
{
|
||||||
string name = PrototypeCache.Display.GetName();
|
var name = PrototypeCache.Display.GetName();
|
||||||
if (name != null)
|
if (name != null)
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// It's also possible for DisplayNameAttribute to be used without setting a name. If a user does that, then DisplayName will
|
// If DisplayAttribute does not specify a name, we'll fall back to the property name.
|
||||||
// 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.
|
|
||||||
return base.GetDisplayName();
|
return base.GetDisplayName();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
|
||||||
{
|
|
||||||
public class DisplayNameAttribute : Attribute
|
|
||||||
{
|
|
||||||
public virtual string DisplayName { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
#if K10
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.ModelBinding
|
|
||||||
{
|
|
||||||
public class ReadOnlyAttribute
|
|
||||||
{
|
|
||||||
public bool IsReadOnly { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Loading…
Reference in New Issue