I am building a small craigslist/ebay type marketplace for my local community.
我正在为当地社区建立一个小型的craigslist / ebay类型的市场。
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.
大多数网站都是简单的CRUD操作。我启用了个人用户帐户,当有人将项目发布到市场时,我想将他们当前的用户ID绑定到帖子。我已设置字段但如何自动附加登录用户的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.
我在产品数据库表中有一个字段,它将保存ApplicationUser_Id字符串值,但我不知道如何设置它。
This is my create product controller. Would I put in that userID logic here?
这是我的创建产品控制器。我会在这里放入那个userID逻辑吗?
[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);
}
1 个解决方案
#1
0
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
我有一段时间没有使用Entity,所以我认为这应该是正确的
#1
0
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
我有一段时间没有使用Entity,所以我认为这应该是正确的