Home > Blockchain >  Post in ASP.net MVC using ASP-for
Post in ASP.net MVC using ASP-for

Time:10-01

I'm working on a small library website where you should be able to post a comment to each individual book. The problem is in my view that I can't say "Asp-for='BookComment.Name'" since my BookComment is a list in my Book Model

My Book Model

public class Book
{
    [Key]
    public int BookID { get; set; }
    [Required]
    [Column(TypeName = "Varchar(75)")]
    public string Title { get; set; }
    [Required]
    [Column(TypeName = "Varchar(75)")]
    public string Author { get; set; }
    [Required]
    [Column(TypeName = "Varchar(13)")]
    public string Isbn { get; set; }
    [Required]
    [Column(TypeName = "Varchar(50)")]
    public string Publisher { get; set; } 
    public int Sites { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string Summary { get; set; }
    public string Picture { get; set; }
    public DateTime AddedDate { get; set; }
    public int Stars { get; set; }
    public List<BookCategory> BookCategory { get; set; } = new List<BookCategory>();
    public List<BookComment> BookComment { get; set; } = new List<BookComment>();
}

BookComment Model:

public class BookComment
{
    [Key]
    public int BookCommentID { get; set; }
    [Required]
    public int BookID { get; set; }
    [Column(TypeName = "Varchar(50)")]
    public string Name { get; set; }
    [Column(TypeName = "Varchar(100)")]
    public string Email { get; set; }
    public string Review { get; set; }
    public DateTime Date { get; set; }
    public decimal Stars { get; set; }
}

My Book Controller

public class BookController : Controller
{
    private readonly ApplicationDbContext _db;

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

    public IActionResult Book(int? id)
    {
        var book = _db.Books.Include(o => o.BookComment).FirstOrDefault(p => p.BookID == id);
        return View(book);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Book(BookComment comment)
    {
        _db.bookComments.Add(comment);
        _db.SaveChanges();
        return RedirectToAction("Book");
    }
}

Snippet of my form

@model LibraryNew.Models.Book    
<h5 class="mt-4">Tilføj en anmeldelse</h5>
        <p>Din email vil ikke blive offentliggjort</p>
        <form asp-action="Book" method="post"></form>
        <div class="my-3">
            <div class="form-group">
                <label for="exampleFormControlSelect1">Antal stjerner:</label>
                <select class="form-control col-md-1" id="exampleFormControlSelect1">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="1">5</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="exampleFormControlInput1">Email address</label>
            <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="[email protected]">
        </div>
        <div class="form-group">
            <label for="exampleFormControlInput1">Email address</label>
            <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="[email protected]">
        </div>
        <div class="form-group">
            <label for="exampleFormControlTextarea1">Example textarea</label>
            <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
        </div>

I can't say <select asp-for="BookComment.Stars"

Any help is appreciated. If any further information is needed please let me know!

CodePudding user response:

In your view code try replacing @model LibraryNew.Models.Book with @model LibraryNew.Models.BookComment so you can have access to it in asp-for since what you are trying to do is post a single BookComment to add it to a book in your controller.

It looks like what is needed is to be able to use different models in a single page to achieve this you can create a single class that contains the models you will need for a single page for example

public class BookLibrary
{
    public Book Book { get; set; } 
    public BookComment BookComment { get; set; } 
    public Author Author{ get; set; }
}

then in your view code you use @model LibraryNew.Models.BookLibrary and in your asp-for you will be able to access BookComment by using Model.BookComment

  • Related