簡體   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