Question

I've been unable to get default editor templates to work in my MVC 4 Razor web application. I've been unable to find any clues. I want to override String, Password, etc. I've tried UIHints in my model (even though you shouldn't have to for default editor templates), ensured I'm using EditorFor in my view and have placed the Editor Templates under ~/Views/Shared/EditorTemplates. I'm stuck. Any help would be appreciated. Thanks!

    public class LoginModel
{
    [Required(AllowEmptyStrings = false)]
    [StringLength(128)]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required(AllowEmptyStrings = false)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }

    [HiddenInput()]
    public Guid UserEmailAddressId { get; set; }

    [HiddenInput()]
    public bool IsVerified { get; set; }
}

Part of my view where I'm binding to my model...

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>RegisterModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.PrimaryEmailAddress)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PrimaryEmailAddress)
        @Html.ValidationMessageFor(model => model.PrimaryEmailAddress)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.ConfirmPassword)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ConfirmPassword)
        @Html.ValidationMessageFor(model => model.ConfirmPassword)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.CultureId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.CultureId, (IEnumerable<SelectListItem>)ViewBag.Cultures, " -- Select -- ", null)
        @Html.ValidationMessageFor(model => model.CultureId)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.TimeZoneId)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.TimeZoneId, (IEnumerable<SelectListItem>)ViewBag.TimeZones, " -- Select -- ", null)
        @Html.ValidationMessageFor(model => model.TimeZoneId)
    </div>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>

}

In the Password.cshtml file under ~/Views/Shared/EditorTemplates/Password.cshtml

@Html.Password("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "k-textbox" })
THE EDITOR IS WORKING
Was it helpful?

Solution

Turns out I was looking at the wrong View. The actual View was using @Html.TextBoxFor which is why the Default Editor Template for the password was not rendering. You need to use @Html.EditorFor to invoke the default editor template.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top