I have a @model MyModel on a razor view. I know if I have a FirstName string defined in MyModel with a
[Display(Name = "First Name:")]
property, that I can use
@Html.LabelFor(m => m.FirstName, new { @class = "mylabelstyle", title = "Enter first name." })
I am using radio button lists with code such as the following:
[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));
}
}
I do the above so that in a confirm page I can do something like:
<div class="display-label">
Referred From:
</div>
<div class="display-field">
@Html.Raw(MyNamespace.Models.MyModel.ReferralDictionary.referralDictionary[Model.Referral])
</div>
which outputs the selection (say, “Google”) for example.
The above code is working, so if I mistyped something that’s not my problem.
My problem is that in my razor view, I have am doing this:
<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>
If you notice, I am putting, for example, “Google” as text in the model class, and writing out “Google” again in the view.
For one word this is not a big deal (nor for one radio button list). However, I am using a lot of radio button lists, and many of my choices are short sentences. So, keeping the view and the model class text in “synch” is starting to become a nightmare when I make changes.
I tried using the @Html.Raw to output the individual dictionary item with no success. So I am stuck.
Any ideas?
I think this can help: http://jonlanceley.blogspot.com/2011/06/mvc3-radiobuttonlist-helper.html