我在.cshtml文件中使用以下内容作为第一行:
@model Dictionary<string, List<Chapter>>
获取方法工作正常。在方法I之后,模型I中的数据显示在UI中,期望字典填充未发生的字典。以下是控制器发布方法:
[HttpPost]
public ActionResult Update(Dictionary<string, List<Chapter>> model)
我的字典大小为8,但我得到的结果大小为2,其中列表为空,键为"controller"
和"action"
。
我该如何解决这个问题?
查看:
@model Dictionary<string, List<Chapter>>
using (Html.BeginForm("Update", "Controller", FormMethod.Post))
{
<fieldset>
<table class="LP3">
@foreach (string className in Model.Keys)
{
@Html.HiddenFor(a => Model[className])
List<Chapter> cList = Model[className];
<tr>
<th style="width: 50px;">
</th>
<th style="text-align: left">
@className
</th>
<th style="width: 50px;">
</th>
<th style="text-align: left">
</th>
<th style="width: 50px;">
</th>
<th style="text-align: left">
</th>
</tr>
for (int i = 0; i < cList.Count / 3; i++)
{
<tr>
@Html.HiddenFor(a => Model[className][3 * i].id)
@Html.HiddenFor(a => Model[className][3 * i].className)
<td style="width: 50px;">
@Html.CheckBoxFor(chapterItem => Model[className][3 * i].included)
</td>
<td style="text-align: left">
@cList[3 * i].name
@Html.HiddenFor(a => Model[className][3 * i].name)
</td>
@Html.HiddenFor(a => Model[className][3 * i + 1].id)
@Html.HiddenFor(a => Model[className][3 * i + 1].className)
<td style="width: 50px;">
@Html.CheckBoxFor(chapterItem => Model[className][3 * i + 1].included)
</td>
<td style="text-align: left">
@cList[3 * i + 1].name
@Html.HiddenFor(a => Model[className][3 * i + 1].name)
</td>
@Html.HiddenFor(a => Model[className][3 * i + 2].id)
@Html.HiddenFor(a => Model[className][3 * i + 2].className)
<td style="width: 50px;">
@Html.CheckBoxFor(chapterItem => Model[className][3 * i + 2].included)
</td>
<td style="text-align: left">
@Model[className][3 * i + 2].name
@Html.HiddenFor(a => Model[className][3 * i + 2].name)
</td>
</tr>
}
if (cList.Count % 3 == 2)
{
<tr>
@Html.HiddenFor(a => Model[className][cList.Count - 2].id)
@Html.HiddenFor(a => Model[className][cList.Count - 2].className)
<td style="width: 50px;">
@Html.CheckBoxFor(chapterItem => Model[className][cList.Count - 2].included)
</td>
<td style="text-align: left">
@cList[cList.Count - 2].name
@Html.HiddenFor(a => Model[className][cList.Count - 2].name)
</td>
@Html.HiddenFor(a => Model[className][cList.Count - 1].id)
@Html.HiddenFor(a => Model[className][cList.Count - 1].className)
<td style="width: 50px;">
@Html.CheckBoxFor(chapterItem => Model[className][cList.Count - 1].included)
</td>
<td style="text-align: left">
@Model[className][cList.Count - 1].name
@Html.HiddenFor(a => Model[className][cList.Count - 1].name)
</td>
</tr>
}
else if (cList.Count % 3 == 1)
{
<tr>
@Html.HiddenFor(a => Model[className][cList.Count - 1].id)
@Html.HiddenFor(a => Model[className][cList.Count - 1].className)
<td style="width: 50px;">
@Html.CheckBoxFor(chapterItem => Model[className][cList.Count - 1].included)
</td>
<td style="text-align: left">
@Model[className][cList.Count - 1].name
@Html.HiddenFor(a => Model[className][cList.Count - 1].name)
</td>
</tr>
}
<tr style="height: 50px;">
</tr>
}
</table>
</fieldset>
}
}
答案 0 :(得分:1)
您可以使用ViewModel来保存字典对象。像这样创建一个ViewModel:
public class YourViewModel
{
public Dictioanry<string, List<Chapter>> chapters {get; set; }
}
<强> CSHTML:强>
@model ViewModel.YourViewModel
@using ViewModels;
您可以按照您的方式设置视图,访问权限会有所不同。使用Model
的位置,将其替换为Model.chapters
并在控制器中设置模型。
另外,请删除cshtml中的@Html.HiddenFor(a => Model[className])