我有一個表單來創建新的登錄信息。在帖子中,我必須將登錄信息與安全問題和答案綁定在一起。我已將安全問題限制在數據庫的下拉列表中。如何將問題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" })
屬性'Answer1'需要typeof'string',而不是'List',屬性'Question1'和'Question2'需要是'int'類型並使用'@ Html.DropDownListFor(m => m。 Question1,新的SelectList(...))' –
謝謝,將退房並取回。我有三個表格,一個用於登錄信息,一個用於安全問題,另一個用於存儲安全問題的答案。因此,一個用戶將有兩個問題ID的答案,我如何將它綁定到模型?這是我的問題在這裏 – user1221765
我會很快發佈一個答案,但我認爲你的'答案'表有UserID,QuestionID和答案的字段? –