如何在字典中显示单选按钮列表中的文本?

时间:2012-02-04 21:00:55

标签: asp.net-mvc-3 razor

我在剃须刀视图上有一个@model MyModel。我知道我是否在MyModel中使用

定义了FirstName字符串
[Display(Name = "First Name:")]

属性,我可以使用

@Html.LabelFor(m => m.FirstName, new { @class = "mylabelstyle", title = "Enter first name." })

我正在使用带有如下代码的单选按钮列表:

[Required(ErrorMessage = "Please tell us how you heard of us.")]
        [Display(Name = "How did you hear about us?")]
        public ReferralList Referral { get; set; }
        public enum ReferralList
        {
            Referral_Google,
            Referral_OtherSearch,
            Referral_Website,
            Referral_Friend,
            Referral_Television,
            Referral_Radio,
            Referral_Other
        }
        public class ReferralDictionary
        {
            public static readonly Dictionary<ReferralList, string> referralDictionary = new Dictionary<ReferralList, string>
            {
                { ReferralList.Referral_Google, "Google" },
                { ReferralList.Referral_OtherSearch, "Bing / Yahoo / AOL" },
                { ReferralList.Referral_Website, "Website1.com / Website2.com" },
                { ReferralList.Referral_Friend, "Friend / Relative / Co-worker" },
                { ReferralList.Referral_Television, "Television" },
                { ReferralList.Referral_Radio, "Radio" },
                { ReferralList.Referral_Other, "Other" },
            };
            static string ConvertReferral(ReferralList referrallist)
            {
                string name;
                return (referralDictionary.TryGetValue(referrallist, out name))
                    ? name : referrallist.ToString();
            }

            static void Main()
            {
                Console.WriteLine(ConvertReferral(ReferralList.Referral_Google));
                Console.WriteLine(ConvertReferral(ReferralList.Referral_OtherSearch));
                Console.WriteLine(ConvertReferral(ReferralList.Referral_Website));
                Console.WriteLine(ConvertReferral(ReferralList.Referral_Friend));
                Console.WriteLine(ConvertReferral(ReferralList.Referral_Television));
                Console.WriteLine(ConvertReferral(ReferralList.Referral_Radio));
                Console.WriteLine(ConvertReferral(ReferralList.Referral_Other));
            }
        }

我做以上操作,以便在确认页面中我可以执行以下操作:

<div class="display-label">
     Referred From:
</div>
<div class="display-field">
     @Html.Raw(MyNamespace.Models.MyModel.ReferralDictionary.referralDictionary[Model.Referral])
</div>

输出选择(例如,“Google”)。

上面的代码正在运行,所以如果我错误输入了不是我的问题。

我的问题是,在我的剃刀视图中,我正在这样做:

<td class="radio_cell">
    @Html.LabelFor(m => m.Referral, new { @class = "mylabelstyle", title = "How did you learn about our services?." })
</td>
<td>
    <span class="radio_validation">@Html.ValidationMessageFor(m => m.Referral)</span>
    <div class="radio">
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_Google")
            Google
        </p>
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_OtherSearch")
            Bing / Yahoo / AOL
        </p>
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_Website")
            Website1.com / Website2.com
        </p>
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_Friend")
            Friend / Relative / Co-worker
        </p>
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_Television")
            Television
        </p>
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_Radio")
            Radio
        </p>
        <p>
            @Html.RadioButtonFor(m => m.Referral, "Referral_Other")
            Other
        </p>
    </div>
</td>

如果您注意到,我会将“Google”作为模型类中的文本,并在视图中再次写出“Google”。

总而言之,这不是什么大问题(也不是一个单选按钮列表)。但是,我使用了很多单选按钮列表,我的很多选择都是短句。因此,当我进行更改时,将视图和模型类文本保持在“同步”状态开始变成一场噩梦。

我尝试使用@ Html.Raw输出单个字典项但没有成功。所以我被卡住了。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

相关问题