views:

21

answers:

2

How can I customize the text that is rendered with the LabelFor() method?

I have a bool field called IsMarried in my model, it currently renders IsMarried on the View. How can I have it say something like: "Married".

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TeacherEvaluation.Models.GuestBookEntry>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 GuestBook Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Guest Book</h2>

    <p>Please sign the Guest Book!</p>

    <% using (Html.BeginForm()) { %>

       <fieldset>
            <legend>Guest Book</legend>

            <%: Html.LabelFor(model => model.Name) %>
            <%: Html.TextBoxFor(model => model.Name) %>

            <%: Html.LabelFor(model => model.Email) %>
            <%: Html.TextBoxFor(model => model.Email) %>

            <%: Html.LabelFor(model => model.Comment) %>
            <%: Html.TextAreaFor(model => model.Comment, new { rows = 6, cols = 30 })%>       

            <%: Html.LabelFor(model => model.IsMale) %>
            <%: Html.CheckBoxFor(model => model.IsMale) %>     

            <div>
                <input type="submit" value="Sign" />
            </div>
        </fieldset>

    <% } %>

</asp:Content>

Thanks!

+1  A: 

Use DisplayNameAttribute over your class property. For example:

class GuestBookEntry
{
      //...

      [DisplayName("Married")]
      public bool IsMarried { get; set; }

      //...
}
ŁukaszW.pl
Hey that's really nice. I'll accept this answer after the 8 minutes are up. Thanks again!
Serg
thanks, good luck with your project...
ŁukaszW.pl
+2  A: 

Add a DisplayNameAttribute to your Model IsMarried property:

[DisplayName("Married")]
public virtual bool IsMarried {get;set;}
GenericTypeTea