2012-12-05 67 views
2

我一直无法使用默认编辑器模板在我的MVC 4 Razor Web应用程序中工作。我一直无法找到任何线索。我想重写字符串,密码等。我已经在我的模型中尝试了UIHints(即使您不需要默认编辑器模板),确保我在我的视图中使用EditorFor并将编辑器模板放置在〜 /查看/共享/ EditorTemplates。我卡住了。任何帮助,将不胜感激。谢谢!我的观点的覆盖默认MVC编辑器模板不起作用

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> 

}

在在〜/查看/共享/ EditorTemplates /密码的Password.cshtml文件.cshtml

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

回答

1

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