[英]Override Default MVC Editor Templates Not Working
我一直无法在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
原来我看错了视图。 实际的View使用的是@ Html.TextBoxFor,这就是为什么密码的默认编辑器模板未呈现的原因。 您需要使用@ Html.EditorFor来调用默认的编辑器模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.