I'm begginer in ASP.NET MVC, so I have many problems. One of them is: how can I get list of books selected user (based on e.g. userId) and display them in view?
public class Book
{
public Book()
{
this.States = new HashSet<State>();
this.UserProfiles = new HashSet<UserProfile>();
}
public int BookId { get; set; }
public string BookName { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public string PublicationLanguage { get; set; }
public virtual ICollection<State> States { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
and
public class UserProfile
{
public UserProfile()
{
this.Books = new HashSet<Book>();
this.webpages_Roles = new HashSet<webpages_Roles>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string UniversityName { get; set; }
public string UniversityAddress { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string PhoneNumber { get; set; }
public Nullable<bool> Activated { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}
Books are added like this:
[HttpGet]
public ActionResult AddBook()
{
Book book = new Book();
return View(book);
}
[HttpPost]
public ActionResult AddBook(Book book)
{
book.UserProfiles.Add(_bookService.GetUserById(WebSecurity.CurrentUserId)); // get currently logged in user
_bookService.AddBook(book);
return View("BookInfo", book);
}
and _bookService.AddBook(book):
public void AddBook(Book book)
{
using (MyContext ctx = new MyContext())
{
ctx.Books.Add(book);
ctx.SaveChanges();
}
}
The problem may be trivial but I'm still learning and I would be grateful for help