0

ラジオボタンのリストを作成するために asp.net mvc3 を使用しています。コントローラーからリストを取得し、ビューに表示する必要があります。ビュー内の対応するリストごとに、YES/NO ラジオ ボタンがあり、リスト内の各項目について、ラジオ ボタンが「はい」の場合は「1」、「0」の場合は最後に文字列を生成する必要があります。

たとえば、リストに10個のアイテムがある場合、それらをビューに表示し(アイテムのデフォルト値はfalse)、送信時に文字列を生成する必要があります。文字列の各文字は各アイテムのブール値に対応しますリストで。

mvc3 でこれを行う方法を誰か教えてもらえますか? 事前に助けてくれてありがとう。

アップデート

これが私が試しているコードです:

私のクラスには2つのプロパティがあります:

public List<Module> lstModules { get; set; } // gives the list of items

public List<bool> IsModuleActivelst { get; set; } //gives the bool values 

ここのコントローラーでは、リストとそれに対応するブール値を作成する必要があります。ここで立ち往生しており、コードを生成できません。とにかく疑似コードを説明します

public class MMController : Controller
{
    [HttpGet]
    public ActionResult Clients()
    {
        //I need to generate the list - using lstModules prop

        // Assign the list with the predefined values and if not just give the default values- using IsModuleActivelst  prop

     }
}

ここでビューを作成します:

 @foreach (var i in Model.lstModules)
{
    <div class="formlabel">
        <div align="right" class="label">

            @Html.LabelFor(model => model.lstModules):</div>
    </div>

    <div class="formelement">
     <label for="radioYes" class="visible-enable" style="cursor:pointer;position:relative;">
                @Html.RadioButtonFor(model => model.IsModuleActivelst, "True", new { @id = "radioYes", @style = "display:none;" })
               <span>Yes</span></label>
                <label for="radioNo" class="visible-disable" style="cursor:pointer;position:relative;">
                @Html.RadioButtonFor(model => model.IsModuleActivelst, "False", new { @id = "radioNo", @style = "display:none;" })
                 <span>No</span></label>
    </div>
}
4

1 に答える 1

1

特定のビューで操作する必要のある情報を表すビューモデルを定義することから始めることをお勧めします。これまで、ユーザーがラジオボタンを使用してモジュールをアクティブにするかどうかを設定する必要があるモジュールのリストについて説明しました(個人的には、True / Falseステータスのチェックボックスを使用しますが、それはあなた自身の決定です)。

public class ModuleViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

public class MyViewModel
{
    public IEnumerable<ModuleViewModel> Modules { get; set; }
}

次に、ビューモデルにデータを入力し、フォームをレンダリングし、フォームの送信を処理する別のアクションを実行するコントローラーを定義できます。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: this information could come from a database or something
            Modules = new[]
            {
                new ModuleViewModel { Id = 1, Name = "module 1", IsActive = true },
                new ModuleViewModel { Id = 2, Name = "module 2", IsActive = true },
                new ModuleViewModel { Id = 3, Name = "module 3", IsActive = false },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content(
            string.Format(
                "Thank you for selecting the following values: {0}",
                string.Join(" ", model.Modules.Select(x => string.Format("model id: {0}, active: {1}", x.Id, x.IsActive)))
            )
        );
    }
}

最後の部分は、ビューを定義することです(~/Views/Home/Index.cshtml):

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Modules)
    <button type="submit">OK</button>
}

そして最後に、Modulesコレクションの各要素に対して自動的にレンダリングされる対応するエディターテンプレート-テンプレートの名前と場所が重要であることに注意してください- ~/Views/Shared/EditorTemplates/ModuleViewModel.cshtml

@model ModuleViewModel

<div>
    @Html.HiddenFor(x => x.Id)
    @Html.HiddenFor(x => x.Name)

    <h2>@Html.DisplayFor(x => x.Name)</h2>
    @Html.Label("IsActiveTrue", "Yes")
    @Html.RadioButtonFor(x => x.IsActive, "True", new { id = Html.ViewData.TemplateInfo.GetFullHtmlFieldId("IsActiveTrue") })
    <br/>
    @Html.Label("IsActiveFalse", "No")
    @Html.RadioButtonFor(x => x.IsActive, "False", new { id = Html.ViewData.TemplateInfo.GetFullHtmlFieldId("IsActiveFalse") })
</div>
于 2012-10-03T11:45:22.793 に答える