覆盖默认MVC编辑器模板不起作用

时间:2012-12-05 02:58:00

标签: asp.net-mvc razor mvc-editor-templates

我无法在我的MVC 4 Razor Web应用程序中使用默认编辑器模板。我一直无法找到任何线索。我想覆盖String,Password等。我在我的模型中尝试了UIHints(即使你不应该使用默认的编辑器模板),确保我在我的视图中使用EditorFor并将编辑器模板放在〜下/查看/共享/ EditorTemplates。我被卡住了。任何帮助,将不胜感激。谢谢!

    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; }
}

我的观点的一部分,我绑定到我的模型...

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

}

在〜/ Views / Shared / EditorTemplates / Password.cshtml下的Password.cshtml文件中

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

1 个答案:

答案 0 :(得分:1)

原来我看错了View。实际的View使用的是@ Html.TextBoxFor,这就是为什么密码的默认编辑器模板没有呈现的原因。您需要使用@ Html.EditorFor来调用默认编辑器模板。