繁体   English   中英

从MVC的下拉列表中获取ID值

[英]Grabbing the ID value from a drop down list in mvc

总体情况是这样的:用户登录时,他们可以从下拉列表中选择一个“组织”进行登录,该下拉列表为每个组织具有唯一的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");
        }
    }

这是我的ViewModel:

    public class LoginViewModel
{
    public Music.Models.Organizations Organizations { get; set; }
}

关于如何获取组织的ID号,然后选择唯一的人员ID号,以便使该会话变量具有任何想法? 非常感谢!

编辑:更新了代码以反映我根据提供的答案所做的更改。

您可以创建一个类,例如带有ID的会话信息,以在登录后立即捕获它。 让您的登录名执行登录验证,然后转到会话信息页面,在该页面上,根据用户可用的组织来选择组织。 将其传递给存储在会话信息中的帮助程序类。

编辑

重新阅读您的问题后,您可以在登录页面上提供组织列表,并根据您当前正在做的事情进行验证,并确保它也与组织匹配,因此只有一个屏幕。

public class LoginCredential
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public int OrganizationId { get; set; }
}

public ActionResult Index()
{
    ViewBag.Organizations = new SelectList(db.Organizations, "OrgID", "OrgName");
    return View();
}

使您的登录页面模型成为登录凭证

@model MyProject.Models.LoginCredential

然后,当您从表单进行提交时,它将传递选定的选项。

[HttpPost, ActionName("Submit")]
public ActionResult Login(LoginCredential credentials)
{    
    String Username = credentials.UserName ;
    String Password = credentials.Password ;
    Int OrganizationId = credentials.OrganizationId;
    ///  Rest of your code goes here
}

根据kadumel的答案创建了ViewModel之后,我仍然遇到与以前相同的错误。 由于用户名和密码都从视图中发回,因此我只需要一个模型即可用于视图中以获取OrgID。 我所做的就是在我看来将@model指令更改为此:

@model Music.Models.Organizations

然后将我的POST中的参数更改为:

public ActionResult SubmitPost(Organizations model, string LoginNID, string LoginPassword)

从那里,我能够通过执行以下操作获得OrgID:

int OrgID = model.OrgID;

通过调试,OrgID最终获得了正确的值,并表示“模型”的值不再为null。 虽然这样做有效,但我不确定为什么会这样,因为我认为使用ViewModel会做同样的事情,因为ViewModel包含Organizations模型。

编辑:我想出了为什么这是可行的,并且使用ViewModel似乎没有:在我看来

@Html.DropDownList("Organization", ViewData["Organizations"] as SelectList, "--Select Organization--")

第一个参数“ Organization”在我的模型中不存在,因此该模型当然将返回null而不提供ID。 当我将其更改为:

@Html.DropDownList("OrgID", ViewData["Organizations"] as SelectList, "--Select Organization--")

它回传了ID,我很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM