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);
}
Yes, you would need to add the user to the product before you save it. Something like:
if (ModelState.IsValid)
{
db.Products.Add(product);
db.Products.ApplicationUser = currentUser; //depending how you have your user defined
db.SaveChanges();
return RedirectToAction("Index");
}
I haven't used Entity in awhile, so I think this should be correct
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.