简体   繁体   中英

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. When the user logs in, I want to grab their ID # from my people table. 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. Here is what I've got in my GET method:

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

Here is my POST method:

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

    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? 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. 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. 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. What I did was change the @model directive in my view to this:

@model Music.Models.Organizations

and then changed the parameters in my POST to:

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

From there, I was able to get the OrgID by doing this:

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. 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.

EDIT: I figured out why this was working and using a ViewModel didn't seem to: in my view I had

@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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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