来自枚举Asp.net MVC的可本地化下拉列表 - c#

我有下拉列表,从下拉列表中填充。我的解决方案项目由两部分组成:域和UI项目。这个枚举是域模型的一部分,因此被放置在域项目中。

public enum ActivityStatus
    {
        InProgress = 0,
        Completed = 1,
        Freezed = 2,
        NotStarted = 3,
        None = 4
    }

我想使用RESX文件本地化UI上的下拉内容。我研究了一些解决方案,他们提议在Enum字段上提供自定义属性。但是我认为本地化超出了我的域模型的范围,因此我想在这里使用这些属性。有没有办法对我的UI进行本地化?

参考方案

我还在项目中本地化了一些枚举,因此,我有一个像这样的类,用于在枚举中创建注释:

public class LocalizedEnumAttribute : DescriptionAttribute
{
    private PropertyInfo _nameProperty;
    private Type _resourceType;

    public LocalizedEnumAttribute(string displayNameKey)
        : base(displayNameKey)
    {
    }

    public Type NameResourceType
    {
        get
        {
            return _resourceType;
        }
        set
        {
            _resourceType = value;

            _nameProperty = _resourceType.GetProperty(this.Description, BindingFlags.Static | BindingFlags.Public);
        }
    }

    public override string Description
    {
        get
        {
            //check if nameProperty is null and return original display name value
            if (_nameProperty == null)
            {
                return base.Description;
            }

            return (string)_nameProperty.GetValue(_nameProperty.DeclaringType, null);
        }
    }
}

我还有一个EnumHelper类可用于我的项目中,以创建本地化的枚举值字典:

public static class EnumHelper
{
    // get description data annotation using RESX files when it has
    public static string GetDescription(Enum @enum)
    {
        if (@enum == null)
            return null;

        string description = @enum.ToString();

        try
        {
            FieldInfo fi = @enum.GetType().GetField(@enum.ToString());

            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attributes.Any())
                description = attributes[0].Description;
        }
        catch
        {
        }

        return description;
    }

    public static IDictionary<TKey, string> GetEnumDictionary<T, TKey>()
        where T : struct 
    {
        Type t = typeof (T);

        if (!t.IsEnum)
            throw new InvalidOperationException("The generic type T must be an Enum type.");

        var result = new Dictionary<TKey, string>();

        foreach (Enum r in Enum.GetValues(t))
        {
            var k = Convert.ChangeType(r, typeof(TKey));

            var value = (TKey)k;

            result.Add(value, r.GetDescription());
        }

        return result;
    }
}

public static class EnumExtensions
{
    public static string GetDescription(this Enum @enum)
    {
        return EnumHelper.GetDescription(@enum);
    }
}

通过此类,您可以在枚举上使用:

public enum ActivityStatus 
{ 
    [LocalizedEnum("InProgress", NameResourceType = typeof(Resources.Messages))]
    InProgress = 0, 

    [LocalizedEnum("Completed", NameResourceType = typeof(Resources.Messages))]
    Completed = 1, 

    [LocalizedEnum("Freezed", NameResourceType = typeof(Resources.Messages))]
    Freezed = 2, 

    [LocalizedEnum("NotStarted", NameResourceType = typeof(Resources.Messages))]
    NotStarted = 3, 

    [LocalizedEnum("None", NameResourceType = typeof(Resources.Messages))]
    None = 4 
}

并为此创建一个组合框,可以在asp.net mvc的控制器上使用,如下所示:

var data = EnumHelper.GetEnumDictionary<ActivityStatus, int>();

jQuery发布不会将数据发布到ASP.NET API控制器 - javascript

我有一次噩梦般的时间通过jquery post将数据发送到ASP.NET Controller。这是JSON.stringify之后的数据:[{"scheduleTaskID":"203","task":"Permit","baselineDate":…

.NET C#Webbrowser填充输入,不带ID或类名 - javascript

我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…

ddl在服务器中未更新-asp.net - javascript

我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……

从JAVA调用方法C#.NET - java

我有一个C#.NET项目中创建的dll,我想从Java程序中调用方法。我想知道是否存在实现此目标的机会,然后遇到了JNA和JNI。我应该使用哪一个?有什么建议么?我只需要在使用C#.NET编写的类中调用方法并处理Java程序的结果即可。 参考方案 这取决于您的应用程序,但是您可以将C#DLL放在服务中,例如WCF并以这种方式将功能公开给Java代码。使用ws…

asp.net mvc中的对象数组数据始终为null - javascript

我需要通过json将对象数组发送到asp.net mvc 2,但是我在mvc控制器中没有得到null对象是这样的entries[1].date = "12/22/2014" entries[1].Ref = "0002" entries[1].Credit = "100" entries[2].da…