I am building a small craigslist/ebay type marketplace for my local community.
Most of the site is simple CRUD operations. I have enabled Individual user accounts and when someone posts an item to the marketplace I'd like to bind their current userID to the post. I have the fields set up but how can I automatically attach the logged in user's Id.
This is my product class
public class Product
{
public string Title { get; set; }
public decimal Price { get; set; }
public string Photo { get; set; }
public string Description { get; set; }
public bool Availability { get; set; }
public string Category { get; set; }
public string Id { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
I have a field in the product database table that will hold the ApplicationUser_Id
string value, but I do not know how to set it.
This is my create product controller. Would I put in that userID logic here?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Title,Price,Description,Availability,Category,Photo")] Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}