How do I store the currently logged in user in my table?

Question!

Please have patience I am new at MVC. I have created a model called "Book" which represents a text book, in my IdentityModel I added this:

public class User : IUser
{

// ...

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

public string UserName { get; set; }

// Code First will use this to create a foreign key in book
public virtual ICollection<Book> Uploaders { get; set; }

}

This created a foreign key in my Books table which is what I wanted. Now in my book controller I simply want to link the book to the user when they hit "create". This is where I am stuck

//
// POST: /Book/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Book book)
{
    if (ModelState.IsValid)
    {
        // Save uploader here
        // book.Uploader = User.Identity;

        db.Books.Add(book);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(book);
}


Answers

on your Book Model:

public virtual User User {get;set;}
public int UserId {get;set;}

and assign that in your Controller.

book.User = User;
By : bluetoft


This video can help you solving your question :)
By: admin