2

我一直无法让默认编辑器模板在我的 MVC 4 Razor Web 应用程序中工作。我一直找不到任何线索。我想覆盖字符串、密码等。我已经在我的模型中尝试过 UIHints(即使你不应该使用默认编辑器模板),确保我在我的视图中使用 EditorFor 并将编辑器模板放在 ~ /视图/共享/编辑器模板。我被困住了。任何帮助,将不胜感激。谢谢!

    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
4

1 回答 1

1

原来我在看错误的视图。实际视图使用的是@Html.TextBoxFor,这就是密码的默认编辑器模板未呈现的原因。您需要使用 @Html.EditorFor 来调用默认的编辑器模板。

于 2012-12-05T15:05:18.287 回答