Skip to content

Commit

Permalink
Merge pull request #41 from paolosanchi/master
Browse files Browse the repository at this point in the history
Fix and features
  • Loading branch information
jgauffin committed Jun 14, 2013
2 parents 4460c86 + bb85f20 commit 81eb143
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DefaultTagBuilderFactory()
public ITagBuilder Create(string tagName)
{
ITagBuilderFactory factory;
if (!_tagBuilders.TryGetValue(tagName, out factory))
if (_tagBuilders.TryGetValue(tagName, out factory))
return factory.Create(tagName);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public GeneratorContext(string name, string fullName, ModelMetadata metadata,
/// <param name="context">The context.</param>
protected GeneratorContext(GeneratorContext context)
{
_viewContext = context.ViewContext;
Name = context.Name;
FullName = context.FullName;
Metadata = context.Metadata;
Expand Down
40 changes: 35 additions & 5 deletions source/Griffin.MvcContrib/Html/Generators/SelectGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Griffin.MvcContrib.Localization;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;

namespace Griffin.MvcContrib.Html.Generators
{
Expand All @@ -11,7 +13,9 @@ public class SelectGenerator : FormTagGenerator
{
protected override IEnumerable<NestedTagBuilder> GenerateTags()
{
if (typeof (Enum).IsAssignableFrom(Context.Metadata.ModelType))
var type = getNonNullableModelType();

if (typeof (Enum).IsAssignableFrom(type))
return GenerateForEnum(Context);

var selectContext = Context as SelectContext;
Expand All @@ -34,21 +38,47 @@ public virtual IEnumerable<NestedTagBuilder> GenerateForEnum(ITagBuilderContext
if (!string.IsNullOrEmpty(Context.Metadata.Description))
selectTag.MergeAttribute("title", Context.Metadata.Description);

foreach (var enumName in Enum.GetNames(Context.Metadata.ModelType))
var names = Enum.GetNames(getNonNullableModelType());
if(context.Metadata.IsNullableValueType)
names = new string[] { string.Empty }.Concat(names).ToArray();

foreach (var enumName in names)
{
var tagBuilder = new NestedTagBuilder("option");
tagBuilder.MergeAttribute("value", enumName, true);
if (GetValue() == enumName)
tagBuilder.MergeAttribute("selected", "selected");


var title = LocalizedStringProvider.GetEnumString(Context.Metadata.ModelType, enumName);
tagBuilder.SetInnerText(title);
if (enumName != string.Empty)
{
var title = LocalizedStringProvider.GetEnumString(getNonNullableModelType(), enumName) ?? DefaultUICulture.FormatUnknown(enumName);
tagBuilder.SetInnerText(title);
}

selectTag.AddChild(tagBuilder);
}

return new[] {selectTag};
}

private Type getNonNullableModelType()
{
return getNonNullableModelType(Context.Metadata);
}

/// <remarks>
/// http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx
/// </remarks>
private Type getNonNullableModelType(ModelMetadata modelMetadata)
{
Type realModelType = modelMetadata.ModelType;

Type underlyingType = Nullable.GetUnderlyingType(realModelType);
if (underlyingType != null)
{
realModelType = underlyingType;
}
return realModelType;
}
}
}
14 changes: 14 additions & 0 deletions source/Griffin.MvcContrib/Localization/DefaultUICulture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,19 @@ public static bool Is(CultureInfo culture)
return culture.Name.StartsWith(_culture.Name);
return _culture.Name.StartsWith(culture.Name);
}

/// <summary>
/// Format the name as the default format for unknown translations:
/// ex: [en-US:ProperyName]
/// </summary>
/// <param name="name">String to format</param>
/// <returns>Formatted name</returns>
public static string FormatUnknown(string name)
{
var uiCultureName = "?";
if (!DefaultUICulture.IsActive)
uiCultureName = CultureInfo.CurrentUICulture.Name;
return string.Format("[{0}: {1}]", uiCultureName, name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,8 @@ protected TypePromptCollection GetLanguage(CultureInfo culture)
if (_languages.TryGetValue(culture, out prompts))
return prompts;

prompts = LoadLanguage(culture);
if (prompts == null)
{
prompts = new TypePromptCollection(culture);
_languages.Add(culture, prompts);
}
prompts = LoadLanguage(culture) ?? new TypePromptCollection(culture);
_languages.Add(culture, prompts);
}

return prompts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attribute

if (metadata.DisplayName == null)
{
metadata.DisplayName = Translate(containerType, propertyName);
if (!DefaultUICulture.IsActive && metadata.DisplayName == null)
metadata.DisplayName = string.Format("[{0}: {1}]", CultureInfo.CurrentUICulture.Name, propertyName);
metadata.DisplayName = Translate(containerType, propertyName) ?? DefaultUICulture.FormatUnknown(propertyName);
}

if (metadata.Watermark == null)
Expand Down

0 comments on commit 81eb143

Please sign in to comment.