delete
delete

Reputation:

Customizing the text that renders with the LabelFor() helper method

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!

Upvotes: 1

Views: 271

Answers (2)

djdd87
djdd87

Reputation: 68456

Add a DisplayNameAttribute to your Model IsMarried property:

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

Upvotes: 2

Łukasz W.
Łukasz W.

Reputation: 9755

Use DisplayNameAttribute over your class property. For example:

class GuestBookEntry
{
      //...

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

      //...
}

Upvotes: 0

Related Questions