简体   繁体   中英

asp.net mvc ListBoxFor - MultiSelectList does not apply the selected item

The tags are multi-selectable and are retrieved from the database.

This is my action in the controller :

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);
    }

and the view:

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

The thing is that the selected item does not apply! The multi-select element is filled fine but there is no preselection!

Have you tried passing the Id instead of the Name for the selected tags:

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

It can also be a problem with the name of the ListBox being the same as the MultiselectList, you can try to change the name of the tag list that you are passing:

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

And pass that list:

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

I changed the action and every thing is ok. this is my action in controller:

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM