使用显示(创建<li>时的名称)

时间:2017-07-20 15:02:09

标签: c# asp.net asp.net-mvc razor enums

我正在尝试本地化一个开源项目,但我坚持使用枚举字符串。

我的Enum.cs是;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Web;


namespace TestSite.Domain.Models

    {
    public enum SortSpan
        {
            /// <summary>
            /// Default value
            /// </summary>
            [Display(Name = "Hepsi")]
            All = 0,

            /// <summary>
            /// Span aralığını 1 saate indirger
            /// </summary>
            [Display(Name = "Saat")]
            Hour,

            /// <summary>
            /// Span aralığını 1 güne indirger
            /// </summary>
            [Display(Name = "Gün")]
            Day,

            /// <summary>
            /// Span aralığını 1 haftaya indirger
            /// </summary>
            [Display(Name = "Hafta")]
            Week,

            /// <summary>
            /// Span aralığını 1 aya indirger
            /// </summary>
            [Display(Name = "Ay")]
            Month,

            /// <summary>
            /// Limits search span to ~90 days
            /// </summary>
            [Display(Name = "Mevsim")]
            Quarter,

            /// <summary>
            /// Limits search span to 1 year
            /// </summary>
            [Display(Name = "Yıl")]
            Year
        }
    }

我为我的枚举SortSpan上的每个字符串赋予[Display(Name =“...”)]。 但在视图中,它总是看到字符串名称不是DisplayName。

以下是观点;

   @*Top Sort Span Buttons*@
        @if (Model.Context != null && Model.Sort == TestSite.Domain.Models.SortAlgorithm.Top)
        {
            <div>
                <ul class="tabmenu submenu">
                    @{
                        var spans = new SortSpan[] { SortSpan.Day, SortSpan.Week, SortSpan.Month, SortSpan.Quarter, SortSpan.Year, SortSpan.All };
                        foreach (var span in spans)
                        {
<li class='@(Model.Span.HasValue && Model.Span.Value == span ? "selected" : "disabled")'>
@Html.RouteLink(span.ToString(), Model.Submissions.RouteName, new { sort = (Model.Sort == null ? "" : Model.Sort.ToString().ToLower()), span = span.ToString().ToLower() })
</li>
                        }
                    }
                </ul>
            </div>
        }

这是实时截图; It still shows as Day, Week, Month..

但是我想用Gün,Ay,Yıl改变它们,你可以看到[Display(Name =“Gün)] ...

谢谢..

更新:完整的视图代码(.cshtml)

@using Voat.Domain.Models

@model Voat.Models.ViewModels.SubmissionListViewModel

@{
    ViewBag.Title = Model.Title;
    ViewBag.Description = Model.Description;
    int counter = 0;
}

@section PageHeader {
    @if (Model.IsActualSubverse)
    {
        @Html.Partial("_SubverseStylesheetLink", Model.Context.Name, new ViewDataDictionary())
    }
}

<style type="text/css">
    body > .content .link .rank {
        width: 3.2ex;
    }
</style>

<div id="container">
    @if (Model.IsActualSubverse)
    {
        @Html.Action("SidebarForSelectedSubverse", "Subverses", new { selectedSubverse = Model.Context.Name })
    }
    else if (Model.Context != null && Model.Context.Type == Voat.Domain.Models.DomainType.Set)
    {
        @Html.Action("Sidebar", "Set", new { name = Model.Context.FullName })
    }
    else
    {
        @Html.Partial("~/Views/Shared/Sidebars/_SidebarFrontpage.cshtml")
    }

    <div class="content" role="main">

    @*Top Sort Span Buttons *@
    @if(Model.Context != null && Model.Sort == Voat.Domain.Models.SortAlgorithm.Top)
    {
        <div>
            <ul class="tabmenu submenu">
                @{
                    var spans = new SortSpan[] { SortSpan.Day, SortSpan.Week, SortSpan.Month, SortSpan.Quarter, SortSpan.Year, SortSpan.All };
                    Type type = typeof(SortSpan);
                    foreach (var span in spans)
                    {
                        SortSpan sortSpan = SortSpan.All;                               
                        Type type = typeof(SortSpan);
                        MemberInfo[] memberInfo = type.GetMember(sortSpan.ToString());
                        object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
                        string name = ((DisplayAttribute)attributes[0]).Name;   
                        <li class='@(Model.Span.HasValue && Model.Span.Value == span ? "selected" : "disabled")'>@Html.RouteLink(name, Model.Submissions.RouteName, new { sort = (Model.Sort == null ? "" : Model.Sort.ToString().ToLower()), span = name.ToLower() })</li>
                    }
                }
            </ul>
        </div>
    }

        <div class="sitetable linklisting">
            @if (Model.IsActualSubverse)
            {
                @Html.Action("StickiedSubmission", "Subverses", new { subverseName = Model.Context.Name })
            }
            else if (Model.Context == null || (Model.Context != null && Model.Context.Type != DomainType.Set))
            {
                @Html.Partial("~/Views/Welcome/_FirstTimeVisitorWelcome.cshtml")
                @Html.Action("FeaturedSub", "Home")
                @Html.Action("StickiedSubmission", "Home")
            }

            @if (Model.Submissions.Count <= 0)
            {
                Html.RenderPartial("_DefaultMessage", new ViewDataDictionary() {{"selectedSubverse", Model.Context.Name}});
            }

            @foreach (var submission in Model.Submissions)
            {
                @Html.Partial("~/Views/Shared/Submissions/_Submission.cshtml", submission, new ViewDataDictionary() { { "CssClass", new string[] { counter % 2 == 0 ? "even" : "odd" } } })
                counter++;
            }
        </div>

        @{
            Model.Submissions.RouteData = Request.RequestContext.RouteData.Values;
            if (Model.Sort != null)
            {
                Model.Submissions.RouteData["sort"] = Model.Sort.ToString().ToLower();
            }
            if (Model.Span != null)
            {
                Model.Submissions.RouteData["span"] = Model.Span.ToString().ToLower();
            }
            if (!String.IsNullOrEmpty(Request.QueryString["frontpage"]))
            {
                Model.Submissions.RouteData["frontpage"] = Request.QueryString["frontpage"];
            }
        }
        @Html.DisplayFor(model => Model.Submissions, "IPaginatedList")

        @*<div class="pagination-container">
            <ul>
                @if (Model.Submissions.HasPreviousPage)
                {
                    <li class="btn-whoaverse-paging">
                        @Html.RouteLink("< prev", Model.Submissions.RouteName, new {
                       page = (Model.Submissions.PageIndex - 1),
                       sort = (Model.Sort == null ? "" : Model.Sort.ToString().ToLower()),
                       time = (Model.Span == null ? "" : Model.Span.ToString().ToLower()),
                       frontpage = Request.QueryString["frontpage"]
                   }, new {rel = "prev"})
                    </li>
                }

                @if (Model.Submissions.HasNextPage)
                {
                    <li class="btn-whoaverse-paging">
                        @Html.RouteLink("next >", Model.Submissions.RouteName, new {
                       page = (Model.Submissions.PageIndex + 1),
                       sort = (Model.Sort == null ? "" : Model.Sort.ToString().ToLower()),
                       time = (Model.Span == null ? "" : Model.Span.ToString().ToLower()),
                       frontpage = Request.QueryString["frontpage"]
                   }, new { rel = "next" })
                    </li>
                }
            </ul>
        </div>*@

        @if (!Model.IsActualSubverse)
        {
            <div>
                <a href="/random">rastgele evren getir</a> veya <a href="/randomnsfw">rastgele NSFW evren getir </a>
            </div>
        }
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

这对我有用。

SortSpan sortSpan = SortSpan.All;                               
Type type = typeof(SortSpan);
MemberInfo[] memberInfo = type.GetMember(sortSpan.ToString());
object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
string name = ((DisplayAttribute)attributes[0]).Name;

我使用了此链接中的答案Getting attributes of Enum's value

[更新]这是合并的代码。它没有经过测试,只是使用文本编辑器进行了编辑。

 @*Top Sort Span Buttons *@
    @if(Model.Context != null && Model.Sort == TestSite.Domain.Models.SortAlgorithm.Top)
    {
        < div >
            < ul class="tabmenu submenu">
                @{
                    var spans = new SortSpan[] { SortSpan.Day, SortSpan.Week, SortSpan.Month, SortSpan.Quarter, SortSpan.Year, SortSpan.All };
                    Type type = typeof(SortSpan);
                    foreach (var span in spans)
                    {
                        MemberInfo[] memberInfo = type.GetMember(span.ToString());
                        object[] attributes = memberInfo[0].GetCustomAttributes(typeof(DisplayAttribute), false);
                        string name = ((DisplayAttribute)attributes[0]).Name;
                        <li class='@(Model.Span.HasValue && Model.Span.Value == span ? "selected" : "disabled")'>@Html.RouteLink(name, Model.Submissions.RouteName, new { sort = (Model.Sort == null ? "" : Model.Sort.ToString().ToLower()), span = name.ToLower() })</li>
                    }
                }
            </ul>
        </div>
    }

答案 1 :(得分:0)

您最好使用EnumHelper.GetSelectList辅助方法

<ul class="tabmenu submenu">
    @{    
        foreach (SelectListItem span in EnumHelper.GetSelectList(typeof(SortSpan)))
        {
            <li class='@(Model.Span.HasValue && Model.Span.Value.ToString() == span.Value ? "selected" : "disabled")'>
                @Html.RouteLink(span.Text, Model.Submissions.RouteName, new { sort = (Model.Sort == null ? "" : Model.Sort.ToString().ToLower()), span = span.Text.ToLower() })
            </li>
        }
    }
</ul>