【发布时间】:2016-08-31 06:58:31
【问题描述】:
我目前正在制作一个项目,用户应该能够将其他用户添加到组中。我制作了一个文本框,用户可以在其中输入他想要添加的其他用户的电子邮件地址。我的计划是将此文本与数据库中的表进行比较,以查看它当前是否存在。
这就是我在控制器中尝试这样做的方式:
[HttpPost]
public ActionResult Manage(GroupManage gm)
{
HttpCookie groupId = new HttpCookie("selectBoxValue");
groupId = Request.Cookies["selectBoxValue"];
int user = Convert.ToInt32(groupId.Value);
if (gm.addUser == gm.ApplicationUser.Email)
{
var groupmember = new GroupUser { ApplicationUserId = gm.ApplicationUser.Id, GroupId = user };
db.GroupUsers.Add(groupmember);
db.SaveChanges();
}
return View();
}
运行时出现错误:
对象引用未设置为对象的实例。
在我的调试器中,ApplicationUser.Email 的值是null(我在if 语句中使用它来比较)。尽管gm.addUser 包含正确的电子邮件地址。所以我从文本框中得到了正确的输入。但是我不明白如何将我的输入与数据库进行比较。
【问题讨论】:
-
数据库中的值在哪里?
-
我希望数据库中的值可以通过 ApplicationUser 访问,但在我的调试器中结果为 null。我哪里错了?
标签: c# asp.net entity-framework visual-studio