简体   繁体   English

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

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

The big picture is this: when a user logs in, they can select an "organization" to log into from a drop down list, which has a unique ID # for each organization. 总体情况是这样的:用户登录时,他们可以从下拉列表中选择一个“组织”进行登录,该下拉列表为每个组织具有唯一的ID#。 When the user logs in, I want to grab their ID # from my people table. 用户登录后,我想从我的人员表中获取其ID#。 The problem is, the person could be in the table twice, like this: 问题是,该人可能在桌子上两次出现过,如下所示:

ID Name     OrgID
1  Brandon  1
2  Brandon  2

So, to grab the appropriate person ID, I need to check against the OrgID also. 因此,要获取适当的人员ID,我还需要对照OrgID进行检查。 Here is what I've got in my GET method: 这是我在GET方法中得到的:

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

Here is my POST method: 这是我的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");
        }
    }

Here is my ViewModel: 这是我的ViewModel:

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

Any ideas as to how I can get the organization's ID # and then select the unique person ID # so I can make that a session variable? 关于如何获取组织的ID号,然后选择唯一的人员ID号,以便使该会话变量具有任何想法? Thanks much! 非常感谢!

Edit: updated code to reflect changes I've made from answers provided. 编辑:更新了代码以反映我根据提供的答案所做的更改。

You can create a class of something like session information with the id to capture that immediately after login. 您可以创建一个类,例如带有ID的会话信息,以在登录后立即捕获它。 Have your login perform login verification, then route to the session info page where you select the organization based off users available organization. 让您的登录名执行登录验证,然后转到会话信息页面,在该页面上,根据用户可用的组织来选择组织。 Pass that to your helper class that stores it in session information. 将其传递给存储在会话信息中的帮助程序类。

Edit 编辑

After re-reading your question you can provide a list of organizations at the login page and do a verification against whatever you are doing currently and ensuring it matches organization too so it is only one screen. 重新阅读您的问题后,您可以在登录页面上提供组织列表,并根据您当前正在做的事情进行验证,并确保它也与组织匹配,因此只有一个屏幕。

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();
}

Make the model for your login page a login credential 使您的登录页面模型成为登录凭证

@model MyProject.Models.LoginCredential

Then when you do a submit from your form it will pass in the selected options. 然后,当您从表单进行提交时,它将传递选定的选项。

[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
}

After creating a ViewModel based on kadumel's answer, I was still getting the same error as before. 根据kadumel的答案创建了ViewModel之后,我仍然遇到与以前相同的错误。 Since the username and password were both getting posted back from the view, I only needed 1 model to be used by the view to get the OrgID. 由于用户名和密码都从视图中发回,因此我只需要一个模型即可用于视图中以获取OrgID。 What I did was change the @model directive in my view to this: 我所做的就是在我看来将@model指令更改为此:

@model Music.Models.Organizations

and then changed the parameters in my POST to: 然后将我的POST中的参数更改为:

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

From there, I was able to get the OrgID by doing this: 从那里,我能够通过执行以下操作获得OrgID:

int OrgID = model.OrgID;

From debugging, OrgID is finally getting the correct value, and it says that the value of "model" is no longer null. 通过调试,OrgID最终获得了正确的值,并表示“模型”的值不再为null。 While this works, I'm not completely sure why, since I would think that using a ViewModel would do just the same thing because my ViewModel contained the Organizations model. 虽然这样做有效,但我不确定为什么会这样,因为我认为使用ViewModel会做同样的事情,因为ViewModel包含Organizations模型。

EDIT: I figured out why this was working and using a ViewModel didn't seem to: in my view I had 编辑:我想出了为什么这是可行的,并且使用ViewModel似乎没有:在我看来

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

The first parameter, "Organization" is nothing in my model, so of course the model was going to return null and not give me the ID. 第一个参数“ Organization”在我的模型中不存在,因此该模型当然将返回null而不提供ID。 When I changed it to: 当我将其更改为:

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

It passed back the ID and I was good to go. 它回传了ID,我很好。

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

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