C# 多选下拉项目更新时自动选择属性
在一个页面上,我有一个多选下拉菜单。每当我需要插入数据时,它都可以正常工作,但当我需要更新插入的数据时,就会出现问题 问题每当我单击“我的数据”旁边的“编辑”按钮(在我的案例中为“关于书籍数据”)时,表单中的每个字段都会填满,但“选择”下拉菜单项不会自动选择为先前选择的内容。我必须再次手动重新选择它。更新数据本身的过程很好(一旦我再次选择它) 它使用多对多关系。当我使用复选框时效果很好,但我想在下拉菜单上重新做 控制器C# 多选下拉项目更新时自动选择属性,c#,asp.net,multi-select,C#,Asp.net,Multi Select,在一个页面上,我有一个多选下拉菜单。每当我需要插入数据时,它都可以正常工作,但当我需要更新插入的数据时,就会出现问题 问题每当我单击“我的数据”旁边的“编辑”按钮(在我的案例中为“关于书籍数据”)时,表单中的每个字段都会填满,但“选择”下拉菜单项不会自动选择为先前选择的内容。我必须再次手动重新选择它。更新数据本身的过程很好(一旦我再次选择它) 它使用多对多关系。当我使用复选框时效果很好,但我想在下拉菜单上重新做 控制器 public ViewResult Index(int? Id)
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);
}
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);
}
AuthorOptions
是传递asp项目的工具。
表单本身使用Book
Index.cshtml(删除的其他行,仅剩下表单)
下拉列表中的值
为AuthorId
,Book
模型中的BookAuthors
为IList
并连接到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
,这是一个下拉列表,但是单对单连接)都会被选中,而“我的作者”下拉列表则不会被选中?我错过了什么
编辑1
通过更改asp for=“@Model.Book.BookAuthors”=>asp for=“@Model.Book.BookAuthors[0].AuthorId”
执行此操作以获得选择,但如果书籍数据有多个作者,则从下拉菜单中仅选择一个作者。+下拉菜单不再是“多选”,而是可以通过添加属性“多选”来覆盖,但仍然只能从下拉菜单中选择一项。想出了一个窍门
在viewModel中,创建了一个整型的IList公共IList authoritds
。在.cshtml中,intasp for=”“
使用了新创建的整数列表AuthorIds
。在控制器内部,ViewModel中的AuthorIds
通过\u booksRepository.GetBook(Id).booksauthors.Select(x=>x.AuthorId.ToList()传递代码>
所以这个项目看起来是这样的:
控制器
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);
}
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>
拯救
其他一切都没有改变