多选下拉列表项在更新时自动选择属性

问题描述 投票:0回答:1

在页面上,我有一个多选下拉菜单。每当我需要插入数据时它都能正常工作,但是当我需要更新插入的数据时会出现问题。

问题每当我单击数据旁边的编辑按钮(在我的情况下-关于书籍数据)时,表单内的每个字段都会填充,但选择下拉菜单项不会自动选择为先前选择的。我必须再次手动选择它。更新数据本身的过程工作正常(一旦我再次重新选择它)。

它使用多对多关系。当我使用复选框时工作正常,但是我想在下拉菜单上重新进行选择。

控制器

public ViewResult Index(int? Id)
        {
            SelectList selectList = new SelectList(_authorRepository.GetAllAuthors()
                    .Select(x => new { x.Id, Title = x.Name + " " + x.Lastname }), "Id", "Title");
            BooksIndexViewModel viewModel = new BooksIndexViewModel()
            {
                Books = _booksRepository.GetAllBooks(),
                AuthorOptions = selectList,
                authors = _authorRepository.GetAllAuthors(),
                Book = _booksRepository.GetBook(Id ?? 0),
                publishers = _publisherRepository.GetAllPublishers(),
                indexPage = _dataRepository.Generatedata("Knygos", Id,
                    ControllerContext.RouteData.Values["controller"].ToString())
            };
            return View(viewModel);
        }

AuthorOptions是通过asp项的。表单本身使用Book

Index.cshtml(其他行已删除,仅左行)

<form asp-controller="@Model.indexPage.controller" 
    asp-action="@Model.indexPage.action" 
    asp-route-id="@if (Model.indexPage.routeId.HasValue) {@Model.indexPage.routeId.Value}"  method="post">
        <div class="inputs">
            <div> <input asp-for="@Model.Book.Title" /> </div>
            <div> <select asp-for="@Model.Book.BookAuthors" 
                asp-items="@Model.AuthorOptions" name="author[]"></select> </div>
            <div>
                <select asp-for="@Model.Book.PublisherId"  
                    asp-items="@(new SelectList(Model.publishers, "Id", "Title"))"></select>
            </div>
            <div><input asp-for="@Model.Book.Cost" /></div>
            <div><input asp-for="@Model.Book.Code" /></div>
            <div><input asp-for="@Model.Book.InvNr" /></div>
            <div><input asp-for="@Model.Book.Description" /></div>
        </div>
        <button type="submit">Save</button>
    </form>

im之后的行是<select asp-for="@Model.Book.BookAuthors" asp-items="@Model.AuthorOptions" name="author[]"></select>。它以及整个Form从我的存储库中获取数据。

存储库

public Book GetBook(int Id)
        {
            return db.Books.Include(x => x.BookAuthors).SingleOrDefault(x => x.Id == Id);
        }

下拉菜单中的ValueAuthorIdBookAuthors模型中的BookIList并连接到BookAuthor模型:

public class BookAuthor
    {
        public int BookId { get; set; }
        public Book Book { get; set; }
        public int AuthorId { get; set; }
        public Author Author { get; set; }
    }

所以问题是,为什么每当我(从我的ID中)获取电子书的数据时,所有字段(包括PublisherID就是一个下拉列表,但是单对单连接)都被选中但我的Authors下拉列表却不?我想念什么?

编辑1

通过更改asp-for="@Model.Book.BookAuthors" => asp-for="@Model.Book.BookAuthors[0].AuthorId"如果书籍数据多于1位作者,则从下拉菜单中仅选择1位作者以获取所选BUT的技巧。 +下拉列表不再变为多选,但可以通过添加属性multiple来覆盖,但仍然只能从下拉菜单中选择一项。

c# asp.net multi-select
1个回答
0
投票

想出一个把戏。

在viewModel中创建了一个整数public IList<int> AuthorIds的IList。在.cshtml中,int asp-for=""使用了新创建的整数列表AuthorIds。在控制器内部,通过AuthorIds

传递ViewModel中的_booksRepository.GetBook(Id).BookAuthors.Select(x => x.AuthorId).ToList();

所以项目看起来像这样:

Controller

public ViewResult Index(int? Id)
        {
            BooksIndexViewModel viewModel = new BooksIndexViewModel()
            {
                Books = _booksRepository.GetAllBooks(),
                AuthorOptions = new SelectList(_authorRepository.GetAllAuthors()
                    .Select(x => new { x.Id, Title = x.Name + " " + x.Lastname }), "Id", "Title"),
                Book = _booksRepository.GetBook(Id),
                publishers = _publisherRepository.GetAllPublishers(),
                indexPage = _dataRepository.Generatedata("Knygos", Id,
                    ControllerContext.RouteData.Values["controller"].ToString())
            };
            if (Id != null)
                viewModel.AuthorIds = _booksRepository.GetBook(Id).BookAuthors.Select(x => x.AuthorId).ToList();
            return View(viewModel);
        }

cshtml

<form asp-controller="@Model.indexPage.controller" asp-action="@Model.indexPage.action"
          asp-route-id="@if (Model.indexPage.routeId.HasValue) {@Model.indexPage.routeId.Value}" 
          method="post" class="form grid">
        <div class="inputs">
            <div><input asp-for="@Model.Book.Title" class="w100" /></div>
            <div><select asp-for="@Model.AuthorIds" asp-items="@Model.AuthorOptions"
                name="author[]" multiple></select></div>
            <div><select asp-for="@Model.Book.PublisherId" 
                        asp-items="@(new SelectList(Model.publishers, "Id", "Title"))">
            </select></div>
            <div><input asp-for="@Model.Book.Cost" /></div>
            <div><input asp-for="@Model.Book.Code" /></div>
            <div><input asp-for="@Model.Book.InvNr" /></div>
            <div><input asp-for="@Model.Book.Description" /></div>
        </div>
        <button type="submit">Save</button>
    </form>

没有其他改变。

© www.soinside.com 2019 - 2024. All rights reserved.