好吧,明白了這一點。我用了一個字符串類型來保存剛纔所選擇的安全問題和答案:
public class RegisterFormViewModel
{
...
[Required]
[Display(Name = "Security Question")]
public string SecurityQuestion { get; set; }
[Required]
[Display(Name = "Security Answer")]
public string SecurityAnswer { get; set; }
...
}
然後填充列表如下:
@Html.DropDownListFor(m => m.SecurityQuestion, QuestionHelper.GetSecurityQuestions())
使用這個幫手:
public static IEnumerable<SelectListItem> GetSecurityQuestions()
{
return new[]
{
new SelectListItem { Value = "What was your childhood nickname?", Text = "What was your childhood nickname?"},
new SelectListItem { Value = "What is the name of your favorite childhood friend?", Text = "What is the name of your favorite childhood friend?"},
new SelectListItem { Value = "What school did you attend for sixth grade?", Text = "What school did you attend for sixth grade?"},
new SelectListItem { Value = "In what city or town did your mother and father meet?", Text = "In what city or town did your mother and father meet?"},
new SelectListItem { Value = "What is the first name of the boy or girl that you first kissed?", Text = "What is the first name of the boy or girl that you first kissed?"},
new SelectListItem { Value = "What is the name of a college you applied to but didn't attend?", Text = "What is the name of a college you applied to but didn't attend?"},
new SelectListItem { Value = "Where were you when you first heard about 9/11?", Text = "Where were you when you first heard about 9/11?"},
new SelectListItem { Value = "What is your grandmother's first name?", Text = "What is your grandmother's first name?"},
new SelectListItem { Value = "What was the make and model of your first car?", Text = "What was the make and model of your first car?"},
new SelectListItem { Value = "In what city and country do you want to retire?", Text = "In what city and country do you want to retire?"}
};
}
然後做了一個阿賈克斯職位是這樣的:
$.post('/Account/Register', $('#registerForm').serialize(), function(){ //success });
jQuery序列化方法發送安全問題所選項目的「值」。我想要的是真正的問題,這就是爲什麼我將Value和Text設置爲相同的東西,否則您可以將它設置爲任何想要獲取的值(如果選擇該項目)。