数据库中的ASP.NET Razor页面下拉列表

时间:2019-05-27 18:18:51

标签: c# asp.net razor

我正在ASP.NET Razor Pages上编写应用程序(这是我的家庭作业)。 这个应用程序的主题是“图书馆”,我已经添加,删除,修改等。 我想做的是。当我添加一本新书时,当它是书的类型时,我想使用数据库中的下拉列表。我一直在寻找这个,但没有找到适合我的解决方案。

所以我的Book课是: Book.cs

public class Book
{
    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Tytuł")]

    public string Title { get; set; }
    [Required]
    [Display(Name = "Autor")]
    public string Author { get; set; }
    [Required]
    [Display(Name = "Gatunek")]
    public string Genre { get; set; }
    [Required]
    [Display(Name = "Rok Wydania")]
    public int ReleaseYear { get; set; }
    [Required]
    [Display(Name = "Okładka")]
    public string Cover { get; set; }
    [Required]
    [Display(Name = "Liczba Stron")]
    public int NumberOfPages { get; set; }
    [Required]
    [Display(Name = "Opis")]
    public string Description { get; set; }
}

接下来我有流派课程

Genre.cs

[Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Gatunek")]
    public string Name { get; set; }

我将其添加到我的ApplicationDbContext

ApplicationDbContext.cs

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }

    public DbSet<Book> Book { get; set; }
    public DbSet<Genre> Genre { get; set; }

AddBook.cshtml.cs

[Authorize(Roles = SD.AdminEndUser)]
public class AddBookModel : PageModel
{
    private readonly ApplicationDbContext _db;

    [BindProperty]
    public Book Book { get; set; }

    public AddBookModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public IActionResult OnGet()
    {
        return Page();
    }

    public async Task <IActionResult> OnPostAsync(ServiceType ServiceType)
    {
        if(!ModelState.IsValid)
        {
            return Page();
        }

        _db.Book.Add(Book);
        await _db.SaveChangesAsync();

        return RedirectToPage("Index");

    }

然后 AddBook.cs

<form method="post">
<div class="border backgroundWhite">
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.Title"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.Title" class="form-control" />
        </div>
        <span asp-validation-for="Book.Title" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.Author"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.Author" class="form-control" />
        </div>
        <span asp-validation-for="Book.Author" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.Genre"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.Genre" class="form-control" />
        </div>
        <span asp-validation-for="Book.Genre" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.ReleaseYear"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.ReleaseYear" class="form-control" />
        </div>
        <span asp-validation-for="Book.ReleaseYear" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.NumberOfPages"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.NumberOfPages" class="form-control" />
        </div>
        <span asp-validation-for="Book.NumberOfPages" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.Cover"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.Cover" class="form-control" />
        </div>
        <span asp-validation-for="Book.Cover" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label asp-for="Book.Description"></label>
        </div>
        <div class="col-5">
            <input asp-for="Book.Description" class="form-control" />
        </div>
        <span asp-validation-for="Book.Description" class="text-danger"></span>
    </div>
    <div class="form-group row">
        <div class="col-5 offset-2">
           <partial name="_AddAndBackToListButton" />
        </div>
    </div>
</div>

我与Db有正常的联系。我只是不知道接下来我该怎么做才能使dropdownlist。我对控制器解决方案不感兴趣!

有什么想法吗?

谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这可能是重复的:

但是无论如何,我会尽力回答:

  • 如果使用Razor语法编写,则可以使用与上面的链接相同的解决方案。创建一个HtmlDropdownFor,然后在List<Genre>(类型列表或类型词典,无论您需要哪种迭代方法)上,对参数执行LINQ查询
  • 否则,您可以在每个需要它的HTML表单中手动编写一个<select></select>输入。显然,这比较耗时,但是您可以在w3 schools: HTML Form Elements上找到有关如何执行此操作的更多信息,特别是有关<select>的部分