asp.net mvc ListBoxFor - MultiSelectList不应用所选项

本文关键字:应用 选项 MultiSelectList net mvc ListBoxFor asp | 更新日期: 2023-09-27 18:02:43

标签是多重选择的,从数据库中检索。

这是我在控制器中的动作:

public ActionResult EditDocument(long documentId, string returnURL)
    {
        ViewBag.returnURL = returnURL;
        int userId = AccountController.GetLoggedInId(Session);
        string pageUrl = "Document/EditDocument";
        if (IsDocumentAccessible(documentId,pageUrl) && CheckOperation("EditDocument",pageUrl))
        {
            try
            {
                spGetDocumentById_Result found = db.spGetDocumentById(documentId).ToArray()[0];
                EditDocumentViewModel model = new EditDocumentViewModel()
                {
                    DocumentId = found.DocumentId,
                    DocumentNumber = found.DocumentNumber,
                    DocumentDate = found.DocumentDate,
                    DocumentLevelType = (int)found.DocumentLevelTypeId,
                    AddressSrc = found.Address
                };
                List<spGetTagsOfDocument_Result> tags = db.spGetTagsOfDocument(documentId).ToList();
                foreach (spGetTagsOfDocument_Result tag in tags)
                {
                    model.selectedTags.Add(tag.TagName);
                }
                List<spGetAllDocumentLevelType_Result> DocumentLevelTypeList = db.spGetAllDocumentLevelType(userId, pageUrl).ToList();
                ViewBag.DocumentLevelTypeId = new SelectList(DocumentLevelTypeList, "DocumentLevelTypeId", "DocumentLevelTypeName");
                List<spGetAllTags_Result> TagList = db.spGetAllTags(AccountController.GetLoggedInId(Session), "Document/AddDocument").ToList();
                ViewBag.tags = new MultiSelectList(TagList, "TagId", "TagName", model.selectedTags.ToArray());
                return View(model);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        }
        return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
    }

和视图:

<div class="form-group">
                            @Html.LabelFor(model => model.tags, new { @class = "col-sm-3 control-label" })
                            <div class="col-sm-8">
                                @Html.ListBoxFor(model => model.tags, ViewBag.tags as MultiSelectList, new { @class = "form-control" })
                            </div>
                        </div>

问题是所选项不适用!多选元素填充得很好,但是没有预选!

asp.net mvc ListBoxFor - MultiSelectList不应用所选项

您是否尝试传递所选标签的Id而不是Name:

foreach (spGetTagsOfDocument_Result tag in tags)
{
    model.selectedTags.Add(tag.TagId);
}

它也可以是一个问题与ListBox的名称是相同的MultiselectList,你可以尝试改变你正在传递的标签列表的名称:

 ViewBag.tagList = new MultiSelectList(TagList, "TagId", "TagName", model.selectedTags.ToArray());

传递列表:

@Html.ListBoxFor(model => model.tags, ViewBag.tagList as MultiSelectList, new { @class = "form-control" })

我更改了动作,一切正常。这是我在控制器中的动作:

public ActionResult EditDocument(long documentId, string returnURL)
    {
        ViewBag.returnURL = returnURL;
        int userId = AccountController.GetLoggedInId(Session);
        string pageUrl = "Document/EditDocument";
        if (IsDocumentAccessible(documentId, pageUrl) && CheckOperation("EditDocument", pageUrl))
        {
            try
            {
                spGetDocumentById_Result found = db.spGetDocumentById(documentId).ToArray()[0];
                EditDocumentViewModel model = new EditDocumentViewModel()
                {
                    DocumentId = found.DocumentId,
                    DocumentNumber = found.DocumentNumber,
                    DocumentDate = found.DocumentDate,
                    DocumentLevelType = (int)found.DocumentLevelTypeId,
                    AddressSrc = found.Address
                };
                List<spGetTagsOfDocument_Result> tags = db.spGetTagsOfDocument(documentId).ToList();
                //foreach (spGetTagsOfDocument_Result tag in tags)
                //{
                //    model.selectedTags.Add(tag.TagName);
                //}
                List<spGetAllDocumentLevelType_Result> DocumentLevelTypeList = db.spGetAllDocumentLevelType(userId, pageUrl).ToList();
                ViewBag.DocumentLevelTypeId = new SelectList(DocumentLevelTypeList, "DocumentLevelTypeId", "DocumentLevelTypeName", model.DocumentLevelType);
                List<spGetAllTags_Result> TagList = db.spGetAllTags(AccountController.GetLoggedInId(Session), "Document/AddDocument").ToList();
                //List<spGetAllTags_Result> selectedTags = TagList.Where(x => tags.Any(y => y.TagId == x.TagId)).ToList();
                IEnumerable<SelectListItem> items = from tag in TagList
                                                    select new SelectListItem
                                                    {
                                                        Text = tag.TagName.ToString(),
                                                        Value = tag.TagId.ToString(),
                                                        Selected = tags.Any(y => y.TagId == tag.TagId),
                                                    };
                //ViewBag.tags = new MultiSelectList(TagList, "TagId", "TagName", model.selectedTags.ToArray());
                ViewBag.tags = items;
                return View(model);
            }
            catch (Exception)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
        }
        return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
    }