/// <summary> /// 页面标题 /// </summary> [Display(Name = "页面标题")] [Required(ErrorMessage = "{0}不能为空")] [StringLength(20, ErrorMessage = "{0}长度在{2}~{1}个字符之间", MinimumLength = 1)] [DisplayFormat(ConvertEmptyStringToNull = false)] public string title { get; set; } /// <summary> /// 页面索引 /// </summary> [Display(Name = "页面索引")] [Required(ErrorMessage ="{0}不能为空")] [StringLength(20,ErrorMessage ="{0}长度在{2}~{1}个字符之间",MinimumLength =1)] [DisplayFormat(ConvertEmptyStringToNull = false)] public string titleindex { get; set; }
为什么title上的Required和StringLength属性不起作用?而Display又起作用?
模板代码:
<div class="form-group"> @Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.titleindex, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.titleindex, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.titleindex, "", new { @class = "text-danger" }) </div> </div>
生成的代码:
<div class="form-group"> <label class="control-label col-md-2" for="title">页面标题</label> <div class="col-md-10"> <input class="form-control text-box single-line" id="title" name="title" type="text" value=""> <span class="field-validation-valid text-danger" data-valmsg-for="title" data-valmsg-replace="true"></span> </div> </div>
<div class="form-group"> <label class="control-label col-md-2" for="titleindex">页面索引</label> <div class="col-md-10"> <input class="form-control text-box single-line input-validation-error" data-val="true" data-val-length="页面索引长度在1~20个字符之间" data-val-length-max="20" data-val-length-min="1" data-val-required="页面索引不能为空" id="titleindex" maxlength="20" name="titleindex" type="text" value="" aria-describedby="titleindex-error" aria-invalid="true"> <span class="text-danger field-validation-error" data-valmsg-for="titleindex" data-valmsg-replace="true"><span id="titleindex-error" class="">页面索引不能为空</span></span> </div> </div>
看生成的二段代码,title中的属性没有生效。
经过测试,title,Title只要是这个关键字,都会不起作用,不论大小写。
不知你们有没有碰到过?环境:vs2019+.netcore 2.2