0

作成/保存したいビューがあり、モデルの代わりに次のように列挙型を使用します。

public enum RulesEnum
{
    StopAtLight = 1,
    StopAtStreet,
    StopAtCrossWalk,
    WaitAtCrossWalk,
}

モデル

public class RulesModel
{
    public RulesEnum Rules { get; set; }
    public string Comment { get; set; } 
    // some other props that you have
}

景色

    @model RulesModel
    @using (Html.BeginForm()) {
        <input type="radio" name="Rules" value="0" id="Rules1_No" />
        <input type="radio" name="Rules" value="1" id="Rules1_Yes" />
        <label for="Rules1">Stop at Light</label>
        <input type="radio" name="Rules" value="0" id="Rules2_No" />
        <input type="radio" name="Rules" value="1" id="Rules2_Yes" />
        <label for="Rules2">Stop at Street</label>
        <input type="radio" name="Rules" value="0" id="Rules3_No" />
        <input type="radio" name="Rules" value="1" id="Rules3_Yes" />
        <label for="Rules3">Stop at CrossWalk</label>
        <input type="text" name="Rules" id="Rules4" />
        <label for="Rules4">Wait at CrossWalk</label>
        <input type="submit" value="Submit" />
    }

コントローラーのアクション

[HttpPost]
public ActionResult TheMethodThatAcceptsTheInput(RulesModel model)
{
    // do as you wish here, I assume you already know what to do here
    // at this point, if the user selects a radio button 
    // then the model.Rules will show it, as an enum value of course
    return View(model);
}

この前のスニペットは、各列挙値に異なるコントロールが関連付けられていることを示しています。したがって、本質的に、列挙値ごとに異なるコントロールを定義したいと思います。

KeyValuePairs のリストを作成して、ユーザーが各列挙値に対して選択した値をコントローラーに送り返すことができるようにします。ただし、私の限られた知識では、適切に定義されたモデルをコントローラーに戻す方法しか知りません。KeyValuePairs のリストを作成し、ユーザーが送信ボタンを押したときにそれらをコントローラーに送り返すにはどうすればよいですか?

AutoMapper や ViewModels などのさまざまな代替案を調べましたが、特定の問題がこれらのソリューションに関連しているとは思いません。私はかなり新しいのですが、この問題を解決するための最良の方法について誰かがガイダンスを提供できますか?

ありがとう!

更新: Von の例を見て、私のものを少し修正しました。唯一の違いは、列挙型のリスト (つまり、4 つの列挙値 = 4 つのラジオ ボタン) が必要ないことですが、各列挙値を質問にしたいのですが、例:

光で止まる?はい/いいえ (2 つのラジオ ボタン) 通りで停止しますか? はい/いいえ (2 つのラジオ ボタン) 横断歩道で停止しますか? はい/いいえ (2 つのラジオ ボタン) 横断歩道で待ちますか? 30 秒 (テキスト ボックス)

どうすればよいかわからないのは、たとえば、ユーザーが送信をクリックすることです。どういうわけか各質問を見て、コントローラーに送信するリストを作成する必要があるため、次のようになります。

[(StopAtLight, "false"),
 (StopAtStree, "true"),
 (StopAtCrossWalk, "false"),
 (WaitAtCrossWalk, "40")]

Vonが投稿したものと同様のモデルが期待されているため、ユーザーが送信を押したときにこのリストを作成する方法がわかりません。

この詳細情報が私の状況を理解するのに役立つことを願っています。ご協力ありがとうございます。

4

1 に答える 1

0

KeyValuePairs で何をしたかったのかわかりませんが、質問のマークアップに基づいて、列挙型をページのオプションとして表示し、入力用のテキストボックスをいくつか用意して、コントローラーに送信します。その場合は、次のことができます。

モデル

public class RulesModel
{
    public bool StopAtLight { get; set; }
    public bool StopAtStreet { get; set; }
    public bool StopAtCrossWalk { get; set; }
    public string WaitAtCrossWalk { get; set; }
    // some other props that you have
}

景色

@model RulesModel
@using (Html.BeginForm()) {
    <h1>Stop at Light</h1>
    <input type="radio" name="StopAtLight" value="false" id="Rules1_No" />
    <label for="Rules1_No">No</label>
    <input type="radio" name="StopAtLight" value="true" id="Rules1_Yes" />
    <label for="Rules1_Yes">Yes</label>
    <h1>Stop at Street</h1>
    <input type="radio" name="StopAtStreet" value="false" id="Rules2_No" />
    <label for="Rules2_No">No</label>
    <input type="radio" name="StopAtStreet" value="true" id="Rules2_Yes" />
    <label for="Rules2_Yes">Yes</label>            
    <h1>Stop at CrossWalk</h1>
    <input type="radio" name="StopAtCrossWalk" value="false" id="Rules3_No" />
    <label for="Rules3_No">No</label>
    <input type="radio" name="StopAtCrossWalk" value="true" id="Rules3_Yes" />
    <label for="Rules3_Yes">Yes</label>            
    <h1>Wait at CrossWalk</h1>
    <input type="text" name="WaitAtCrossWalk" id="WaitAtCrossWalk" />
    <input type="submit" value="Submit" />
}

コントローラーのアクション

[HttpPost]
public ActionResult TheMethodThatAcceptsTheInput(RulesModel model)
{
    // if model.StopAtLight == false then the user selected No otherwise it was Yes
    // same goes for the rest of the radio buttons
    return View(model);
}
于 2013-03-22T02:25:25.933 に答える