内部模型我有数据注释:
[Required(ErrorMessageResourceName = "SelectCategory", ErrorMessageResourceType = typeof(MyProject.Properties.Resources)), Range(1, int.MaxValue, ErrorMessageResourceName = "SelectCategory", ErrorMessageResourceType = typeof(MyProject.Properties.Resources))]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
和两种情况:
Situation1
内部控制器我
List<Category> categories = db.GetCategories();
ViewBag.CategoryId = categories;
内部视图:
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-6">
@Html.DropDownListFor(model => model.CategoryId, new SelectList(ViewBag.CategoryId, "Id", "Name", 2), htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
Html结果:
<div class="form-group">
<label class="control-label col-md-3" for="CategoryId">Category</label>
<div class="col-md-6">
<select class="form-control" data-val="true" data-val-number="The field Category must be a number." data-val-range="Select category" data-val-range-max="2147483647" data-val-range-min="1" data-val-required="Select category" id="CategoryId" name="CategoryId">
<option value="0">---Select---</option>
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3">Cat3</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="CategoryId" data-valmsg-replace="true"></span>
</div>
</div>
我在'select'元素中有验证属性,但没有选中的元素
Situation2:
内部控制器我
List<Category> categories = db.GetCategories();
ViewBag.CategoryId = new SelectList(categories, "Id", "Name", 2);
内部视图:
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-6">
@Html.DropDownList("CategoryId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
Html结果:
<div class="form-group">
<label class="control-label col-md-3" for="CategoryId">Category</label>
<div class="col-md-6">
<select class="form-control" id="CategoryId" name="CategoryId">
<option value="0">---Select---</option>
<option value="1">Cat1</option>
<option selected="selected" value="2">Cat2</option>
<option value="3">Cat3</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="CategoryId" data-valmsg-replace="true"></span>
</div>
</div>
我在'select'元素中没有验证属性,但是我选择了元素
有人能解释我的错误吗?如何获得验证属性和选择的选项?
答案 0 :(得分:1)
首先,您不能将ViewBag
属性的名称用作模型属性。将ViewBag
属性更改为(例如)ViewBag.CategoryList
其次,您categories
似乎包含ID=0
和Name="Select"
的项目,因此您的所有选项都有一个值(零是一个整数,因此它的有效),您将永远不会收到错误信息。从categories
中删除该项目并使用
@Html.DropDownListFor(m => m.CategoryId, new SelectList(ViewBag.CategoryList, "Id", "Name"), "--Select--", new { @class = "form-control" })
这将呈现
<select class="form-control" data-val="true" data-val-number="The field Category must be a number." data-val-range="Select category" data-val-range-max="2147483647" data-val-range-min="1" data-val-required="Select category" id="CategoryId" name="CategoryId">
<option value>---Select---</option>
<option value="1">Cat1</option>
<option value="2">Cat2</option>
<option value="3">Cat3</option>
</select>
请注意,第一个选项没有值,因此如果选择此选项,将显示Required
验证错误。
最后,要在首次渲染页面时设置所选项目,请在传递视图之前将其分配给控制器中的属性
model.CategoryId = 2;
return View(model);
默认选择第3个选项(“Cat2”)。