0

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.Phone1model=>model.Phone2model=>model.Phone3のままにしてみましたが、このように設定すると、3 つのラジオ ボタンすべてを選択できます。

4

1 に答える 1

0

3 つすべてのラジオ ボタンを選択することについて話すと、それらがチェックボックスであるかのように話しているように聞こえます。同じグループと見なされるラジオ ボタンは、同時にすべてを選択することはできず、グループごとに 1 つのみを選択できます (ただし、おそらく既にご存知であり、私は誤解しています)。

Stackoverflow のどこかで読んだことから、モデルのプロパティ名とビューのフィールドの名前を一致させる必要があることがわかりました。たとえば、次のようなビューにラジオボタンがあるとします。

@Html.RadioButton("stuffRadio", 1)
@Html.RadioButton("stuffRadio", 2)
@Html.RadioButton("stuffRadio", 3)
@Html.RadioButton("stuffRadio", 4)

そして、フォームが投稿されたときのコントローラーで:

[HttpPost]
public ActionResult TheAction(Model model, string stuffRadio)
{
    //dostuff to model and other things
    string value = stuffRadio;
    //dostuff to the selected radioStuff :)
}

とにかく、私が誤解している場合は、お返ししたいと思います。あなたは各ラジオボタンを区別したいと言っていますが、その理由はよくわかりません。ポストアクションとコントローラーにすべての電話番号を取得できるようにするためですか?

私自身の例を使用すると、次のようになります。

@Html.RadioButton("stuffRadioGroup1", 1)
@Html.RadioButton("stuffRadioGroup2", 2)
@Html.RadioButton("stuffRadioGroup3", 3)
@Html.RadioButton("stuffRadioGroup4", 4)

しかし、もう一度すべてのグループ選択を読みたい場合は、もちろん、次のようにパラメーターを使用して同じことをコントローラーに行う必要があります。

[HttpPost]
public ActionResult TheAction(Model model, 
    string stuffRadioGroup1, string stuffRadioGroup2, 
    string stuffRadioGroup3, string stuffRadioGroup4)
{
    //dostuff to model and other things
    string value1 = stuffRadioGroup1;
    string value2 = stuffRadioGroup2;
    string value3 = stuffRadioGroup3;
    string value4 = stuffRadioGroup4;
    //dostuff to the selected radioStuff :)
}

繰り返しますが、私が誤解しているとしたら、もちろん訂正してください ;) 後で!

##########編集####################

まず第一に、非表示フィールドを送信したい場合は、フォーム内にある必要があり、その特定の非表示フィールドの名前をパラメーターとして同じ名前でコントローラーに渡す必要があります。例えば:

@(Html.BeginForm("SomeAction", "Controller")
{
    <input type="hidden" name="derp" value="1234" />
    @Html.RadioButtonFor(model => model.PreferredPhone, Model.Phone3, new { id = "Phone3", @onclick = "PrefferedPhone(this)" })
    <button type="submit">submit</button>
}

その隠しフィールドもパスする必要がある場合、コントローラーは次のようになります。

[HttpPost]
public ActionResult SomeAction(Model mymodel, string derp, FormCollection collection)
{
    int number = Convert.toInt32(derp);
    //now number should be '1234'

    string phone3 = collection.Get("Phone3"); 
    //now you have the phone number and the hidden field telling you if that number is preffered

    TryUpdateModel(mymodel); //populate the model if it hasn´t already been populated

    if(ModelState.IsValid)
    {
        //do stuff
    else
    {
        //TODO: here you should set the model with the stuff from derp and collection before returning
        return View("SameViewAsBefore", mymodel);
    }
}

ご覧のとおり、私は FormCollection をそこに入れました。これは、別のアプローチを取るためです。これがお役に立てば幸いです。そうでない場合は、いつでももう一度質問してください:) (私は時々物事を誤解することが知られています)

于 2012-11-29T22:07:04.330 に答える