使用当前用户ID在自定义表中创建新行

时间:2019-04-21 14:56:37

标签: c# asp.net-mvc

我想从“用户”表中检索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);
    }

此代码没有任何反应,

0 个答案:

没有答案