-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved checking for numbers and undefined values at client side. Sa…
…mple extended for enum types support. Tests supplemented.
- Loading branch information
Showing
13 changed files
with
352 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Web.Mvc; | ||
using System.Web.Mvc.Html; | ||
|
||
namespace ExpressiveAnnotations.MvcWebSample.Misc | ||
{ | ||
public static class Extensions | ||
{ | ||
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression) | ||
{ | ||
return EnumDropDownListFor(htmlHelper, expression, null); | ||
} | ||
|
||
public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes) | ||
{ | ||
var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); | ||
var type = Nullable.GetUnderlyingType(metadata.ModelType) ?? metadata.ModelType; | ||
if (!type.IsEnum) | ||
throw new ArgumentException("Given parameter expression has to indicate enum type.", "expression"); | ||
var values = Enum.GetValues(type).Cast<TEnum>(); | ||
|
||
var items = values.Select(value => new SelectListItem | ||
{ | ||
Text = GetEnumDisplayText(value), | ||
Value = Convert.ToInt32(value).ToString(CultureInfo.InvariantCulture), | ||
Selected = value.Equals(metadata.Model) | ||
}); | ||
|
||
if (metadata.IsNullableValueType) // if the enum is nullable, add an empty item to the collection | ||
items = new[] {new SelectListItem()}.Concat(items); | ||
|
||
return htmlHelper.DropDownListFor(expression, items, htmlAttributes); | ||
} | ||
|
||
private static string GetEnumDisplayText<TEnum>(TEnum value) | ||
{ | ||
var field = value.GetType().GetField(value.ToString()); | ||
var attrib = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; | ||
return attrib != null ? attrib.GetName() : value.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/ExpressiveAnnotations.MvcWebSample/Models/Stability.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace ExpressiveAnnotations.MvcWebSample.Models | ||
{ | ||
public enum Stability | ||
{ | ||
[Display(ResourceType = typeof(Resources), Name = "High")] | ||
High, | ||
[Display(ResourceType = typeof(Resources), Name = "Low")] | ||
Low, | ||
[Display(ResourceType = typeof(Resources), Name = "Uncertain")] | ||
Uncertain, | ||
} | ||
} |
78 changes: 66 additions & 12 deletions
78
src/ExpressiveAnnotations.MvcWebSample/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.