C# 提交验证失败后,未重新填充多个选择类别字段

C# 提交验证失败后,未重新填充多个选择类别字段,c#,validation,razor,model-view-controller,multi-select,C#,Validation,Razor,Model View Controller,Multi Select,我有一个多选下拉选项,允许您选择任意多个值。表单和验证似乎按预期工作,但我注意到,如果我有多个多选选项“selected”(比如5个selected),并且表单在验证后失败,那么多选在验证后只返回1个selected选项。似乎验证无论如何只能返回1个值。如何使表单在表单验证失败后返回所有选定值?(选择全部5个) 我尝试添加将类别数组传递到MultiSelect重载的特殊逻辑。从理论上讲,这应该是可行的,但它似乎没有反映出视图中的这些价值观?验证提交失败后返回所有多选值的最佳方法是什么 我添加了一

我有一个多选下拉选项,允许您选择任意多个值。表单和验证似乎按预期工作,但我注意到,如果我有多个多选选项“selected”(比如5个selected),并且表单在验证后失败,那么多选在验证后只返回1个selected选项。似乎验证无论如何只能返回1个值。如何使表单在表单验证失败后返回所有选定值?(选择全部5个)

我尝试添加将类别数组传递到MultiSelect重载的特殊逻辑。从理论上讲,这应该是可行的,但它似乎没有反映出视图中的这些价值观?验证提交失败后返回所有多选值的最佳方法是什么

我添加了一些屏幕截图,以帮助更好地理解我的控制器和视图代码

提交前

提交后(注意缺少的报告类别字段)

控制器

  private void getReportCategoryList(int[] categoryIds){

            //New Multi Select List. Gets all category combinations 
            var categories = db.Report_Category_NEW.Select(c => new
            {
                CategoryID = c.ReportCategoryID,
                CategoryName = c.ReportCategory
            }).OrderBy(c => c.CategoryName).ToList();

            //Place Categories in ViewData for View()
            if(categoryIds == null)
            {
            //If categoryIds is null, no categoryIds to pre populate
                ViewData["Categories"] = new MultiSelectList(categories, "CategoryID", "CategoryName");
            }
            else
            {
            //If categoryIds is NOT null, pre populate the MultiSelectList using the categoryIds array
                ViewData["Categories"] = new MultiSelectList(categories, "CategoryID", "CategoryName", categoryIds);
            }
        }
查看

                  <div class="form-group">
                        @Html.LabelFor(model => model.Report_Category, "Report Category", htmlAttributes: new { @class = "control-label col-md-2 required" })
                        <div class="col-md-5">
                            @Html.DropDownList("CategoryIds", (MultiSelectList)ViewData["Categories"], new { multiple = "multiple", @class = "select2 form-control" })
                            @Html.ValidationMessageFor(model => model.CategoryIds, "", new { @class = "badge badge-default badge-danger text-left" })
                        </div>
                    </div>

@LabelFor(model=>model.Report_Category,“Report Category”,htmlAttributes:new{@class=“control label col-md-2 required”})
@Html.DropDownList(“CategoryIds”,(MultiSelectList)ViewData[“Categories”],new{multiple=“multiple”,@class=“select2 form control”})
@Html.ValidationMessageFor(model=>model.CategoryIds,“,新的{@class=“badge-badge-default-badge-danger-text-left”})