Michael Edwards
Michael Edwards

Reputation: 6518

ASP.NET MVC 3 Labels Not Displaying

I have a class marked as follows:

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

In the Razor view I am accessing it like so, where MyClass is a property on the model:

 @Html.Label("MyClass.FirstName")

However the value defined in the Display attribute isn't displayed. If I write:

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

This works fine, however for the solution I am working on I have to use the first method. What have I missed on the first method?

UPDATE

Thanks for looking at this question, the problem was caused by the model be altered before the partial view was called. This mean that the model being evaluated against was not the model I was expecting.

The problem is now resolved.

Upvotes: 0

Views: 1319

Answers (2)

CrazyCoderz
CrazyCoderz

Reputation: 1341

Here is an example from one of my projects.

First the view model class

  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>

Upvotes: 0

John x
John x

Reputation: 4031

If you are strongly typing your view with the MyClass model try

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

Upvotes: 1

Related Questions