0

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

2 Answers 2

1

I recommend you to make simple the structure. Just add UserId property to Book and remove virtual collections from both models. Then, using LINQ, collect data you want:

public List<Book> LoadBooksByUserId(int userId)
{
        using (MyContext ctx = new MyContext())
        {
            return ctx.Books.Where(e=>e.UserId == userId).ToList();
        }
}

And you can show data by watching this: Basic CRUD Functionality with the Entity Framework in ASP.NET MVC

Sign up to request clarification or add additional context in comments.

Comments

0

I assume your Book-User relationship is many-to-many, since each object has a collection of another. If this is the case, you should also add the book created to the Books collection of the User. Something like this:

_bookService.GetUserById(WebSecurity.CurrentUserId).Books.Add(book);

in AddBook action. So you will have reference of all books for User. Then just make another method/action to return the books collection of user. For example:

public List<Book> LoadBooksByUser(int userId) 
{
    using (MyContext ctx = new MyContext())
    {
        return ctx.UserProfiles.Where(x => x.UserId = userId).Books.ToList();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.