0

I'm writing Application on ASP.NET Razor Pages(It's my homerwork). Topic of this app is "Library" I have adding,deleting,modifying etc. What i'm trying to do is. When i'm adding a new book, when it's genre of book i want to use dropdown list from database. I have been searching for this but i didn't find working solution for me.

So my Book class is: 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; }
}

Next i have Genre class

Genre.cs

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

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

I'm adding this to my 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");

    }

And 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>

I have working connection with Db. I just don't know what i should do next to make dropdownlist. I'm not interested in solution with controllers!

Any ideas ?

Thank you for assistance!

Archeon
  • 27
  • 2
  • 8

1 Answers1

0

This is a possible repeat of:

But anyway, I'll try and answer:

  • If you're writing it in Razor syntax, you can just use the same solution as the link above. Create an HtmlDropdownFor, and as the argument perform a LINQ query on your List<Genre> (list of of genres, or dictionary of genres, whichever iterable you might need)
  • Otherwise, you can just manually write a <select></select> input inside each of the HTML form that requires it. Obviously this is more time-consuming, but you can find more info on how to do that, at w3 schools: HTML Form Elements, specifically the section about <select>