您好,我正在尝试提交父项和子项,我可以提交父项但不能提交子项,有什么办法可以做到这一点吗?
这是我的代码。
@model IECWeb.Models.CurrencyDay
using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>CurrencyDay</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CurrencyDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CurrencyDate)
@Html.ValidationMessageFor(model => model.CurrencyDate)
</div>
<p />
<table>
<tr>
<th>Currency</th>
<th>Rate</th>
</tr>
@foreach (var item in Model.Currency) {
<tr>
<td>
<div class="editor-field">
@Html.EditorFor(model => item.Name)
@Html.ValidationMessageFor(model => item.Name)
</div>
</td>
<td>
<div class="editor-field">
@Html.EditorFor(model => item.Rate)
@Html.ValidationMessageFor(model => item.Rate)
</div>
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
当我提交时,我得到了 CurrencyDay 对象,但不是 Currency 列表
谢谢 寿司咬。
最佳答案
我建议您使用编辑器模板而不是在您的 View 中编写循环:
<table>
<tr>
<th>Currency</th>
<th>Rate</th>
</tr>
@Html.EditorFor(x => x.Currency)
</table>
然后在将为货币集合的每个元素呈现的相应编辑器模板中(~/Views/Shared/EditorTemplates/CurrencyViewModel.cshtml
):
@model CurrencyViewModel
<tr>
<td>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</td>
<td>
<div class="editor-field">
@Html.EditorFor(model => model.Rate)
@Html.ValidationMessageFor(model => model.Rate)
</div>
</td>
</tr>
请注意,编辑器模板的名称和位置很重要。按照惯例,位置应该是 ~/Views/SomeController/EditorTemplates
(如果模板在 Controller 的操作之间重复使用)或更全局的 ~/Views/Shared/EditorTemplates
.编辑器模板的名称应该是您正在迭代的集合中每个元素的类型的名称。
关于asp.net-mvc-3 - 在 Razor 中提交 parent 和 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8416712/