我想从“用户”表中检索ID并将其与另一个新数据一起插入“活动”表中
public ActionResult Create( Activity activity, RegisterViewModel model)
{
string currentUserId = User.Identity.GetUserId();
var user = db.Activities.SingleOrDefault(u => u.UserId == currentUserId);
ViewData["user"] = currentUserId;
if (ModelState.IsValid)
{
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
activity = new Activity()
{
Name = activity.Name,
Description = activity.Description,
Date = activity.Date,
UserId = (Convert.ToString(user)),
};
db.Activities.Add(activity);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(activity);
}
此代码没有任何反应,