我正在尝试使用一个简单的下拉列表创建一个表单(书),以显示相关表(作者)中的字段。
图书模型:
namespace SimpleDropDownList.Models
{
public class Book
{
[Key]
public int BookID { get; set; }
[StringLength(255)]
[Display(Name = "Book Title")]
public string Title { get; set; }
public int AuthorID { get; set; }
public Author Author { get; set; }
public AuthorViewModel AuthorViewModel { get; set; }
}
}
作者模型:
namespace SimpleDropDownList.Models
{
public class Author
{
[Key]
public int AuthorID { get; set; }
[Display(Name = "First Name")]
[StringLength(50)]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[StringLength(50)]
public string LastName { get; set; }
public ICollection<Book> Books { get; set; } = new List<Book>();
}
}
ViewModel:
namespace SimpleDropDownList.Models
{
[NotMapped]
public class AuthorViewModel
{
//Property to hold the list of authors in the GET
public IEnumerable<SelectListItem> AuthorOptions { get; set; }
//Property to bind the selected author used in the POST
public List<int> SelectedAuthorIds { get; set; }
}
}
BooksController上的Create()方法:
public IActionResult Create()
{
//ViewData["AuthorID"] = new SelectList(_context.Set<Author>(), "AuthorID", "AuthorID");
AuthorViewModel vm = new AuthorViewModel();
vm.AuthorOptions = _context.Book.Select(x => new SelectListItem()
{ Value = x.AuthorID.ToString(), Text = x.Author.LastName }).ToList();
return View(vm);
}
创建视图:
@model SimpleDropDownList.Models.Book
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Book</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AuthorID" class="control-label"></label>
@*<select asp-for="AuthorID" class ="form-control" asp-items="ViewBag.AuthorID"></select>*@
<select asp-for="@Model.AuthorViewModel.SelectedAuthorIds" asp-items="@Model.AuthorViewModel.AuthorOptions"></select>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Model: AuthorViewModel
单击“创建”按钮时,将产生以下错误。
处理请求时发生未处理的异常。 InvalidOperationException:模型项传递到 ViewDataDictionary是类型 'SimpleDropDownList.Models.AuthorViewModel',但这 ViewDataDictionary实例需要以下类型的模型项 'SimpleDropDownList.Models.Book'。
我的期望很简单...打开“创建书”表单并下拉列表以显示作者ID和姓氏。
答案 0 :(得分:0)
将顶部的视图从@model SimpleDropDownList.Models.Book
更改为@model SimpleDropDownList.Models.AuthorViewModel
答案 1 :(得分:0)
现在可以使用了。将属性从Book类移到AuthorViewModel类。现在如下图所示。
图书模型:
namespace SimpleDropDownList.Models
{
public class Book
{
[Key]
public int BookID { get; set; }
[StringLength(255)]
[Display(Name = "Book Title")]
public string Title { get; set; }
public int AuthorID { get; set; }
public Author Author { get; set; }
// Removed this link and added property to AuthorViewModel class
// public AuthorViewModel AuthorViewModel { get; set; }
}
}
作者模型:
namespace SimpleDropDownList.Models
{
public class Author
{
[Key]
public int AuthorID { get; set; }
[Display(Name = "First Name")]
[StringLength(50)]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
[StringLength(50)]
public string LastName { get; set; }
public ICollection<Book> Books { get; set; } = new List<Book>();
}
}
ViewModel:
namespace SimpleDropDownList.Models
{
[NotMapped]
public class AuthorViewModel
{
//Property to hold the list of authors in the GET
public IEnumerable<SelectListItem> AuthorOptions { get; set; }
//Property to bind the selected author used in the POST
public List<int> SelectedAuthorIds { get; set; }
// Added this property to link back to Book class
public Book Book { get; set; }
}
}
将“创建”视图更改为:
public IActionResult Create()
{
// The ViewData line auto created by the scaffolding process
// ViewData["AuthorID"] = new SelectList(_context.Set<Author>(), "AuthorID", "AuthorID");
AuthorViewModel vm = new AuthorViewModel();
vm.AuthorOptions = _context.Book.Select(x => new SelectListItem()
{ Value = x.AuthorID.ToString(), Text = x.Author.LastName }).ToList();
return View(vm);
}