C# asp.net mvc 5下拉列表SelectList无验证属性

C# asp.net mvc 5下拉列表SelectList无验证属性,c#,validation,asp.net-mvc-5,dropdownlistfor,C#,Validation,Asp.net Mvc 5,Dropdownlistfor,在模型内部,我有数据注释: [Required(ErrorMessageResourceName = "SelectCategory", ErrorMessageResourceType = typeof(MyProject.Properties.Resources)), Range(1, int.MaxValue, ErrorMessageResourceName = "SelectCategory", ErrorMessageResourceType = typeof(MyProje

在模型内部,我有数据注释:

    [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; }
有两种情况:

情况1

我有内部控制器

    List<Category> categories = db.GetCategories();
    ViewBag.CategoryId = categories;
    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.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>
<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" 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>
<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”元素中有验证属性,但没有选择的元素

情况2:

我有内部控制器

    List<Category> categories = db.GetCategories();
    ViewBag.CategoryId = categories;
    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.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>
<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" 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>
<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”元素中没有验证属性,但我已选择了元素


谁能给我解释一下怎么了?要使验证属性和选项处于选中状态,我应该怎么做?

首先,ViewBag属性的名称不能与模型属性的名称相同。将ViewBag属性更改为ViewBag.CategoryList

其次,您的类别似乎包含一个ID=0且Name=Select的项,因此您的所有选项都有一个值,即零是一个整数,因此它是有效的,并且您永远不会收到错误消息。从类别中删除该项目并使用

这将导致

<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>

默认情况下,将选择第三个选项AT2。

为什么数据值范围=1不生效?