使用模型数据小问题绑定视图? MVC4

时间:2014-04-07 06:31:35

标签: asp.net-mvc-4

我试图将模型类绑定到我的视图,即强类型。 我的模特课:

 public class LeadSortModel
    {
        public string Contact_Name { get; set; }
        public string Contact_Address { get; set; }

在我看来,我是这样想的:

@Html.LabelFor(m=>m.Contact_Name) 
 @Html.LabelFor(m=>m.Contact_Address)

在我的OUTPUT屏幕上,我好像 联系地址 : Contact_name:..... 但我希望用空格获取联系地址,中间没有“_”。

在模型类属性中,不能使用黑白空格,所以我正在寻找一些更好的替代方案。

任何解决方法都非常有用。否则我应该去ContactAddress和...

谢谢&此致

2 个答案:

答案 0 :(得分:1)

您可以使用[DisplayName("")]命名空间属性中的System.ComponentModel来获取此

  public class LeadSortModel
            {
            [DisplayName("Contact Name")]   
            public string Contact_Name { get; set; }
            [DisplayName("Contact Address")] 
            public string Contact_Address { get; set; 

在视图中

@Html.LabelFor(m=>m.Contact_Name) 

将生成

<label for="Contact_Name">Contact Name</label>

答案 1 :(得分:1)

使用 DataAnnotations DisplayName 属性,如下所示:

public class LeadSortModel
    {
        [DisplayName("Contact Name")]
        public string Contact_Name { get; set; }
        [DisplayName("Contact Address")]
        public string Contact_Address { get; set; }
     }
相关问题