2015-09-04 50 views
0

我有一個表單來創建新的登錄信息。在帖子中,我必須將登錄信息與安全問題和答案綁定在一起。我已將安全問題限制在數據庫的下拉列表中。如何將問題ID和文本值(用於答案)綁定到模型並將其傳遞給數據庫表?將下拉列表選定值及其文本框值綁定到Model MVC5

這裏是我的代碼

型號

[Required] 
[DataType(DataType.EmailAddress)] 
[Display(Name = "Email")] 
public string Email { get; set; } 
[Required] 
[DataType(DataType.Password)] 
[Display(Name = "Password")] 
public string Password { get; set; } 
[Required] 
[Display(Name = "Question1")] 
public List<string> Question1 { get; set; } 
[Required]  
[Display(Name = "Question2")] 
public string Question2 { get; set; } 
[Required] 
[Display(Name = "Answer1")] 
public List<string> Answer1 { get; set; } 
[Required] 
[Display(Name = "Answer2")] 
public string Answer2 { get; set; } 
public SelectList QuestionList { get; set; } 

控制器

[HttpGet] 
public ActionResult NewLogin() 
{ 
    myDB dbCon = new myDB();   
    ViewBag.QuestionList = new SelectList(dbCon.GetQuestion(), "ID", "Value"); 
    return View(); 
} 
[HttpPost] 
public ActionResult NewLogin() 
{ 
    if (ModelState.IsValid) 
    { 
     p.Email = model.Email; 
     p.pass = model.password; 
     // how to get the Question ID and Answer here ? 
     //each user has its own ID where this ID is related to two questions ID in the database 
    } 
    return View(model); 
} 

查看

@Html.DropDownListFor(m=>m.ID,new SelectList(Model.QuestionList,"ID","Value"),"Select a question") 
@Html.TextBoxFor(m => m.Answer1, new { @class = "form-control" }) 

@Html.DropDownListFor(m=>m.ID,new SelectList(Model.QuestionList,"ID","Value"),"Select a question") 
@Html.TextBoxFor(m => m.Answer2, new { @class = "form-control" }) 

enter image description here

+0

屬性'Answer1'需要typeof'string',而不是'List ',屬性'Question1'和'Question2'需要是'int'類型並使用'@ Html.DropDownListFor(m => m。 Question1,新的SelectList(...))' –

+0

謝謝,將退房並取回。我有三個表格,一個用於登錄信息,一個用於安全問題,另一個用於存儲安全問題的答案。因此,一個用戶將有兩個問題ID的答案,我如何將它綁定到模型?這是我的問題在這裏 – user1221765

+0

我會很快發佈一個答案,但我認爲你的'答案'表有UserID,QuestionID和答案的字段? –

回答

0

更改您模型(略屬性)

public class LoginModel 
{ 
    public string Email { get; set; } 
    public string Password { get; set; } 
    public int Question1 { get; set; } // change 
    public int Question2 { get; set; } // change 
    public string Answer1 { get; set; } // change 
    public string Answer2 { get; set; } 
    public SelectList QuestionList { get; set; } 
} 

和Controller

myDB dbCon = new myDB(); 

[HttpGet] 
public ActionResult NewLogin() 
{ 
    // Initialize the model and assign the SelectList 
    LoginModel model = new LoginModel() 
    { 
    QuestionList = SelectList(dbCon.GetQuestion(), "ID", "Value") 
    }; 
    return View(model); 
} 

[HttpPost] 
public ActionResult NewLogin(LoginModel model) 
{ 
    // Save the user and get their ID 
    // Save the values of UserID, Question1, Answer1 
    // and UserID, Question2, Answer2 to your Answers table 
} 

並在視圖

@model LoginModel 
.... 
@Html.DropDownListFor(m => m.Question1, Model.QuestionList, "Select a question") 
@Html.TextBoxFor(m => m.Answer1) 
// ditto for Question2 and Answer2 

旁註:您將可能Ÿ希望有Question2一個foolproof[NotEqualTo]或類似的驗證屬性,因此用戶不能選擇同樣的問題再次

0

這裏改變模型意味着你需要創建一個視圖模型按您的看法和你的模型屬性分別映射到這個視圖模型 。

相關問題