【发布时间】:2015-12-17 13:53:38
【问题描述】:
我对此完全陌生。所以我跟着here的教程。
到目前为止它正在运行,但我想知道如何使用现有作者创建新书。
我的 Book-Model 如下:
public class Book
{
[ScaffoldColumn(false)]
public int BookID { get; set; }
[Required]
public string Title { get; set; }
public int Year { get; set; }
[Range(1, 500)]
public decimal Price { get; set; }
public string Genre { get; set; }
[ScaffoldColumn(false)]
public int AuthorID { get; set; }
// Navigation property
public virtual Author Author { get; set; }
}
AuthorID 将指向Author.AuthorID,如下所示:
public class Author
{
[ScaffoldColumn(false)]
public int AuthorID { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "First Name")]
public string FirstMidName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
在我的BooksController 中如下:
public IActionResult Create()
{
ViewData["AuthorID"] = new SelectList(_context.Author, "AuthorID", "Author");
return View();
}
但是我怎样才能在我的Create.cshtml 中使用这个ViewData["AuthorID"] 作为Form Select Options?
【问题讨论】:
-
您需要提供更多上下文。
Book的模型是什么?您要将所选作者绑定到的Book的属性是什么?
标签: asp.net-mvc model-view-controller razor