5

ユーザーがcheckboxexを選択することで、任意の数のクラブを選択できるビューがあります。クラブは、タイプリスト<ClubModel>のメインモデルのプロパティです。リファクタリング中、私はこれから始めます:

@using (Html.BeginForm())
{
    <fieldset>
        <legend>Voor Select clubs </legend><br />
    <table>
        <tr>
            @for (var i = 0; i < Model.Clubs.Count; i++)
            {
                if (i % 3 == 0)
                {
                    @:</tr><tr> 
                }
                <td>
                    @Html.HiddenFor(model => model.Clubs[i].ClubID)
                    @Html.EditorFor(model => model.Clubs[i].IsAvailable)
                </td> 
                <td>@Html.DisplayFor(model => model.Clubs[i].ClubName)</td>
             }
        </tr>
     </table>
        <input type="submit" value="Submit" />
    </fieldset>
}

これは正常に機能します。モデルは、入力されたClubsプロパティとともに返されます。

次に、>タグを取り出して <td、EditorTemplateに移動します。

@using (Html.BeginForm())
{
    <fieldset>
        <legend>Select Clubs </legend><br />
           <table>
        <tr>
            @for (var i = 0; i < Model.Clubs.Count; i++)
            {
                if (i % 3 == 0)
                {
                    @:</tr><tr> 
                }
               @Html.EditorFor(model=>model.Clubs[i])
             }
        </tr>
     </table>
        <input type="submit" value="Submit" />
    </fieldset>
}

これは引き続き機能します(テンプレートは表示されていません)。

次に、ループもEditorTemplateに移動します。

@using (Html.BeginForm())
{
    <fieldset>
        <legend> Select Clubs</legend><br />
        <EditorFor(model=>model.Clubs,"ListOfClubs")
         <input type="submit" value="Submit" />
    </fieldset>
}

'ListOfClubs'という名前のEditorTemplateを適切に作成します。

@using InvallersManagementMVC3.ViewModels;
@model List<StandInClubModel>
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <table>
        <tr>
            @for (var i = 0; i < Model.Count; i++)
            {
                if (i % 3 == 0)
                {
                    @:</tr><tr> 
                }
                <td>
                    @Html.HiddenFor(model => model[i].ClubID)
                    @Html.EditorFor(model => model[i].IsAvailable)
                </td> 
                <td>@Html.DisplayFor(model => model[i].ClubName)</td>
             }
        </tr>
     </table>
</body>
</html>

これにより、IsAvailableプロパティのチェックボックスが付いたクラブが正しく表示されますが、投稿すると、モデルのClubsプロパティはnullになります。

どこが間違っているのですか?

編集:私は以下を使用してCymenの答えを実装しようとしました:

@Html.EditorFor(model=>model.Clubs,"ClubModel") 

または、これらの要素のリストを渡すときにelementtemplateを指定します。ただし、例外が発生しました。System.InvalidOperationExceptionはユーザーコードによって処理されませんでした。Message=ディクショナリに渡されるモデルアイテムのタイプは'System.Collections.Generic.List`1 [InvallersManagementMVC3.ViewModels.ClubModel]'ですが、このディクショナリはタイプ'InvallersManagementMVC3.ViewModels.ClubModel'のモデルアイテムが必要です。

4

2 に答える 2

7

ビューに渡されたモデルリストを3でグループ化しようとしているようです。したがって、コードをリファクタリングするには、適切なビューモデル=>この特定のビューの要件を反映するモデルを導入することから始めることをお勧めします。

public class GroupedClubs
{
    public IEnumerable<StandInClubModel> Clubs { get; set; }
}

ここで、コントローラーアクション内で、ドメインモデルをこのビューモデルのリストに変換するだけです。

public ActionResult Index()
{
    // This is our domain model. In a real world application
    // it would come from a service layer. I am hardcoding some
    // values here for simplicity
    var clubs = Enumerable.Range(1, 8).Select(x => new StandInClubModel
    {
        ClubID = x,
        ClubName = "club " + x
    });

    // Now we group the list of clubs by 3 in order to simplify
    // our view code and avoid writing some ugly loops and spaghetti code
    // In a real world application I would recommend externalizing this mapping
    // between the domain model and the view model into a separate mapping layer
    // AutoMapper is great for this job 
    var viewModel = clubs
        .Select((club, index) => new { club, index })
        .GroupBy(g => g.index / 3, i => i.club)
        .Select(x => new GroupedClubs
        {
            Clubs = x
        });

    return View(viewModel);
}

あとは、いくつかのビューを作成するだけです。

~/Views/Home/Index.cshtml

@model IEnumerable<GroupedClubs>

@using (Html.BeginForm())
{
    <fieldset>
        <legend> Select Clubs</legend>
        <br />

        <table>
            <tbody>
                @Html.EditorForModel()
            </tbody>
        </table>

        <input type="submit" value="Submit" />
    </fieldset>
}

~/Views/Home/EditorTemplates/GroupedClubs.cshtml

@model GroupedClubs
<tr>
    @Html.EditorFor(x => x.Clubs)
</tr>

~/Views/Home/EditorTemplates/StandInClubModel.cshtml

@model StandInClubModel
<td>
    @Html.HiddenFor(x => x.ClubID)
    @Html.EditorFor(x => x.IsAvailable)
</td>
<td>
    @Html.DisplayFor(x => x.ClubName)
</td>

そしてそれはほとんどすべてです。これで、フォームの送信を処理するコントローラーアクションを実行できます。

[HttpPost]
public ActionResult Index(List<GroupedClubs> clubs)
{
    ... map the view model back to some domain model and pass
        to the service layer for processing
}
于 2011-08-21T15:20:07.923 に答える
3

ClubModelの1つのインスタンスのEditorForを作成し、ASP.NET MVCにレンダリングさせます(反復を実行させます)。ASP.NET MVCには、入力タグ用の特定の名前付け/ IDスキームがいくつかあり、反復でそれらをレンダリングしていません。

したがって、これを使用します-あなたのものと同じですが、テンプレート名を守ってください:

@using (Html.BeginForm())
{
    <fieldset>
        <legend>Select Clubs</legend><br />
        <table>
            <%: EditorFor(model=>model.Clubs, "Club") %>
        </table>
        <input type="submit" value="Submit" />
    </fieldset>
}

そしてEditorFor。上記のリストを渡している場合でも、モデルの単一インスタンス用であることに注意してください。これはASP.NETMVCの「魔法」です。

<tr>
    <td>
        @Html.HiddenFor(model => model.ClubID)
        @Html.EditorFor(model => model.IsAvailable)
    </td>
    <td>@Html.DisplayFor(model => model.ClubName)</td>
</tr>
于 2011-08-21T02:25:13.707 に答える