MVC3 インターネット アプリケーションがあります。
基本的に、3 つのラジオ ボタンがある登録フォームがあります。ラジオ ボタンとヒット ボタンの 1 つをチェックするたびsubmit
に、コントローラーに移動し、データベースにデータを挿入する必要があります。
Registration
フォームのフィールドにも検証があります。
のどこかで、検証エラーの後でもフォームの値を保持するためにstackoverflow
それを読みました。Property names in the Model and the Names of the fields in the view have to be matched
このステートメントは、TextBox と Dropdownlist に対して完全に機能します。
私の質問は次のとおりです。 foreach に個別の名前を付ける方法はradiobutton
?
前述したように、フォームには 3 つのラジオ ボタンがあります。
たとえば、これらは TextBoxes の後にラジオ ボタンが続きます。
Phone 1 // here is the radio button1
Phone 2 // here is the radio button2
Phone 3 // here is the radio button3
私のモデルでは、
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string Phone3 { get; set; }
//also i have a preferred phone--which sets if any of the phone is selected
public bool PreferredPhone { get; set; }
私はこのようなことを試しました
Phone1:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone1, new { id = "Phone1", @onclick = "PrefferedPhone(this)" })
Phone2:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone2, new { id = "Phone2", @onclick = "PrefferedPhone(this)" })
Phone3:
@Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" })
しかし、この宣言はすべてのラジオ ボタンの名前をPreferredPhone
Radio Button ごとに異なる名前を付ける方法は?
RadioButton の名前をmodel=>model.Phone1
、model=>model.Phone2
、model=>model.Phone3
のままにしてみましたが、このように設定すると、3 つのラジオ ボタンすべてを選択できます。