复杂类型列表的EditorTemplate返回null,而其元素的EditorTemplate工作正常

Dab*_*rnl 4 asp.net-mvc mvc-editor-templates razor asp.net-mvc-3

我有一个视图,用户可以通过选择checkboxex选择任意数量的俱乐部.俱乐部是主要模型的属性,类型为List <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>
}
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作:模型将返回一个填充的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>
}
Run Code Online (Sandbox Code Playgroud)

这仍然有效(模板未显示).

现在我想将循环移动到EditorTemplate:

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

我正式创建一个名为'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>
Run Code Online (Sandbox Code Playgroud)

这正确显示了俱乐部的IsAvailable属性的复选框,但现在发布模型的Clubs属性为null!

我哪里错了?

编辑:我尝试使用以下方法实现Cymen的答案:

@Html.EditorFor(model=>model.Clubs,"ClubModel") 
Run Code Online (Sandbox Code Playgroud)

或者在传入这些元素的列表时指定elementtemplate.但是我遇到一个异常:System.InvalidOperationException未被用户代码处理Message =传递给字典的模型项的类型为'System.Collections.Generic.List`1 [InvallersManagementMVC3.ViewModels.ClubModel]',但是这个字典需要"InvallersManagementMVC3.ViewModels.ClubModel"类型的模型项.

Dar*_*rov 7

您似乎正在尝试将传递给视图的模型列表分组3.因此,为了重构代码,我建议您首先引入一个适当的视图模型=>一个反映此特定视图的要求:

public class GroupedClubs
{
    public IEnumerable<StandInClubModel> Clubs { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在在控制器操作中,我们应该简单地将域模型转换为此视图模型的列表:

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);
}
Run Code Online (Sandbox Code Playgroud)

现在剩下的就是写一些观点:

~/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>
}
Run Code Online (Sandbox Code Playgroud)

~/Views/Home/EditorTemplates/GroupedClubs.cshtml:

@model GroupedClubs
<tr>
    @Html.EditorFor(x => x.Clubs)
</tr>
Run Code Online (Sandbox Code Playgroud)

~/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>
Run Code Online (Sandbox Code Playgroud)

这就是全部.现在你可以有一个控制器动作来处理表单提交:

[HttpPost]
public ActionResult Index(List<GroupedClubs> clubs)
{
    ... map the view model back to some domain model and pass
        to the service layer for processing
}
Run Code Online (Sandbox Code Playgroud)