0

「秘密の質問」のユーザー登録時にドロップドラウを作ろうとしています。

SecurityQuestionViewModelのプロパティは、(選択した質問のみを保持する) 文字列にするかIEnumerable<string>、質問のリスト全体を保持する必要がありますか? 後者の場合、投稿されたモデルはどのモデルが選択されたかをどのように知るのでしょうか?

どちらがより良いアプローチであるかに応じて、html ドロップダウン リストを表示し、一番上の項目を事前に選択するコードは何でしょうか (これは必須フィールドであるため、「空白」は選択できないはずです)。

また、ajax を使用してこのフォームを送信します。前者の場合、option:selectedセレクターを使用して、選択した質問をstringモデルの SecurityQuestion プロパティに追加できます。IEnumerable<string>しかし、後者の場合(モデルのプロパティとしてan を使用)でどのように機能するかわかりません

4

1 に答える 1

1

わかりましたこれを理解しました。選択したセキュリティの質問と回答だけを保持するために文字列型を使用しました。

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?"}
            };
    }

次に、次のような ajax 投稿を行いました。

$.post('/Account/Register', $('#registerForm').serialize(), function(){ //success });

jQuery シリアル化メソッドは、セキュリティの質問に対して選択された項目の「値」を送信します。Value と Text を同じものに設定する理由である実際の質問が必要でした。それ以外の場合は、項目が選択された場合に取得したい値を設定できます。

于 2012-12-19T01:42:49.793 に答える