使用LabelFor()辅助方法自定义呈现的文本

时间:2010-08-25 12:46:36

标签: c# asp.net-mvc-2

如何自定义使用LabelFor()方法呈现的文本?

我的模型中有一个名为IsMarried的bool字段,它当前在View上呈现IsMarried。 我怎么能说出这样的话:“已婚”。

<%@ 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>

谢谢!

2 个答案:

答案 0 :(得分:2)

向您的模型IsMarried媒体资源添加DisplayNameAttribute

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

答案 1 :(得分:0)

在您的类属性上使用DisplayNameAttribute。例如:

class GuestBookEntry
{
      //...

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

      //...
}