2012-02-15 77 views
0

我有一个类标记如下:ASP.NET MVC 3个标注不显示

public class MyClass{ 
    [Display(Name="First Name")] 
    public string FirstName{get;set;} 
} 

在Razor视图我访问它像这样,在MyClass的是模型上的属性:

@Html.Label("MyClass.FirstName") 

但是不显示在显示属性中定义的值。如果我写:

@Html.LabelFor(model => model.MyClass.FirstName) 

这工作正常,但对于我正在处理的解决方案,我必须使用第一种方法。第一种方法我错过了什么?

UPDATE

感谢看这个问题,这个问题是由模型导致的局部视图被称为前被改变。这意味着被评估的模型不是我期望的模型。

现在的问题得到解决。

+0

请张贴代码为您的整个视图。我看不出在@model包括为 – CrazyCoderz 2012-02-15 17:54:40

+0

您是否尝试过'@ Html.Label(“Model.MyClass.FirstName”)'? – Lester 2012-02-15 17:55:38

+0

在上面的问题中,您是否在第一个方法中的双引号中使用了参数?它不应该是model.MyClass.FirstName而没有任何引号吗? – Matt 2012-02-15 17:58:44

回答

0
Here is an example from one of my projects. 

首先视图模型类

public class UsersRegisterViewModel 
    { 
     [Display(Name = "E-Mail Address")] 
     [Required(ErrorMessage = "E-Mail address is required")] 
     [Email(ErrorMessage = "Not a valid e-mail address")] 
     [Remote("UserNameIsAvailable", "Validation", ErrorMessage = "Username is not available")] 
     public string UserName { get; set; } 

     [Display(Name = "Password")] 
     [Required(ErrorMessage = "Please enter a password")] 
     public string Password { get; set; } 

     [Display(Name = "Verify Password")] 
     [Required(ErrorMessage = "Please confirm your password")] 
     [Compare("Password", ErrorMessage = "Passwords don't match")] 
     public string VerifyPassword { get; set; } 

     [Required(ErrorMessage = "Please enter a display name")] 
     [Remote("DisplayNameIsAvailable", "Validation", ErrorMessage = "Display name is not avalable")] 
     public string DisplayName { get; set; } 
    } 

Now the View (Ignore the AJAX goo) 

    @model UsersRegisterViewModel 


<div id="user-register" class="site-contol"> 
    <h2>Account Registration</h2> 
    <p></p> 
    @using (Ajax.BeginForm("Register", "Users", null, new AjaxOptions 
                     { 
                      HttpMethod = "post", 
                      UpdateTargetId = "user-registration", 
                      InsertionMode = InsertionMode.Replace, 
                      OnSuccess = "registrationCallBacks.onSuccess", 
                      OnFailure = "registrationCallBacks.onError" 
                     }, new {@id = "frm-sign-in"})) 
    { 
     <ul> 
      <li>@Html.LabelFor(m => m.UserName)</li> 
      <li>@Html.TextBoxFor(m => m.UserName)&nbsp;@Html.ValidationMessageFor(m => m.UserName)</li> 
      <li>@Html.LabelFor(m => m.Password)</li> 
      <li>@Html.PasswordFor(m => m.Password)&nbsp;@Html.ValidationMessageFor(m => m.Password)</li> 
      <li>@Html.LabelFor(m => m.VerifyPassword)</li> 
      <li>@Html.PasswordFor(m => m.VerifyPassword)&nbsp;@Html.ValidationMessageFor(m => m.VerifyPassword)</li> 
      <li>@Html.LabelFor(m => m.DisplayName)</li> 
      <li>@Html.TextBoxFor(m => m.DisplayName)&nbsp;@Html.ValidationMessageFor(m => m.DisplayName)</li> 
      <li> 
       <ul> 
        <li><input type="submit" name="sb-register" value="Create Account"/></li> 
       </ul> 
      </li> 
     </ul> 
    } 
</div> 
1

如果你是强类型化与MyClass模型视图尝试

@Html.LabelFor(model => model.FirstName)