大图是这样的:当用户登录时,他们可以从下拉列表中选择一个“组织”来登录,每个组织都有一个唯一的 ID #。当用户登录时,我想从我的人员表中获取他们的 ID #。问题是,这个人可能在桌子上两次,像这样:
ID Name OrgID
1 Brandon 1
2 Brandon 2
因此,要获取适当的人员 ID,我还需要检查 OrgID。这是我的 GET 方法中的内容:
public ActionResult Index()
{
ViewBag.Organization = new SelectList(db.Organizations, "OrgID", "OrgName");
return View();
}
这是我的 POST 方法:
[HttpPost, ActionName("Submit")]
public ActionResult SubmitPost(string LoginNID, string LoginPassword, LoginViewModel model)
{
//Create new instance of ActiveDirectoryHelper to access its methods
ActiveDirectoryHelper login = new ActiveDirectoryHelper();
//Get Username and Password from forms
String Username = LoginNID;
String Password = LoginPassword;
int OrgID = model.Organizations.OrgID;
//ViewBag initialization for error message
ViewBag.NumTimes = 0;
ViewBag.Message = "Error logging in, please try again.";
//LDAP Authentication
bool LoginPassed = login.IsAuthenticated(Username, Password);
int Organization = organizations.OrgID;
//if Authentication Success enter site, otherwise Return error message
if (LoginPassed == true)
{
//grabs the one person from the People model whose NID is == to Username
var peopleModel = db.People.Single(g => g.NID == Username);
//grabs the peopleID for use in making a session variable
var peopleID = peopleModel.PeopleID;
//sets a session variable to be used that is the logged in person's ID
Session["peopleID"] = peopleID;
return View("LoginSuccess");
}
else
{
ViewBag.NumTimes = 1;
ViewBag.Organization = new SelectList(db.Organizations, "OrgID", "OrgName", organizations.OrgID);
return View("Index");
}
}
这是我的视图模型:
public class LoginViewModel
{
public Music.Models.Organizations Organizations { get; set; }
}
关于如何获取组织的 ID # 然后选择唯一的人员 ID # 以便我可以将其设为会话变量的任何想法?非常感谢!
编辑:更新代码以反映我根据提供的答案所做的更改。