gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:29:45 27 4
gpt4 key购买 nike

我有一个 View ,用户可以通过选择复选框来选择任意数量的俱乐部。俱乐部是主模型的属性,类型为 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>
}

这工作正常:模型返回时带有填充的 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 属性为空!

我哪里出错了?

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

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

或在传入这些元素的列表时指定元素模板。然而我遇到了一个异常(exception):用户代码未处理 System.InvalidOperationException Message=传递到字典中的模型项的类型为“System.Collections.Generic.List`1[InvallersManagementMVC3.ViewModels.ClubModel]”,但此字典需要类型为“InvallersManagementMVC3.ViewModels.ClubModel”的模型项。

最佳答案

您似乎正在尝试将传递给 View 的模型列表按 3 进行分组。因此,为了重构您的代码,我建议您首先引入一个正确的 View 模型 => 一个反射(reflect)此特定 View 要求的 View 模型:

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

现在在 Controller 操作中,我们应该简单地将域模型转换为该 View 模型的列表:

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>

这就是全部了。现在您可以有一个 Controller 操作来处理表单提交:

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

关于asp.net-mvc - 复杂类型列表的 EditorTemplate 返回 null,而其元素的 EditorTemplate 工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7135615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com