我在模型中显示占位符
@Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue,
new { placeholder = ViewData.ModelMetadata.Watermark })
来自Shared\EditorTemplates folder and call it String.cshtml
。
现在我想从模型中设置maxlength,而不是像下面那样放入我的视图。
在我的模型中
[Required(ErrorMessage = "Please Enter Community Code")]
[Display(Prompt = "Enter Company Code")]
[StringLength(6)]
public string COMPANYCODE { get; set; }
查看:
@Html.EditorFor(model => model.COMPANYCODE, new { @maxlength = "4" })
我怎样才能做到这一点?
答案 0 :(得分:0)
我改变了My model和String.cshtml,如下所示
型号:
[Required(ErrorMessage = "Please Enter Community Code")]
[Display(Prompt = "Enter Community Code")]
[AdditionalMetadata("MaxLength", 4)]
public string COMPANYCODE { get; set; }
String.cshtml
@{
string maxLength = ViewData.ModelMetadata.AdditionalValues.ContainsKey("MaxLength")
? ViewData.ModelMetadata.AdditionalValues["MaxLength"].ToString() : null;
}
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { placeholder = ViewData.ModelMetadata.Watermark, @class = "form-control", maxlength = maxLength })
查看:
@Html.EditorFor(model => model.COMPANYCODE)
它起作用了。