Submiting家长和放大器;在剃刀儿童 [英] Submiting Parent & Children in razor
问题描述
你好我试图提交父母和孩子,我可以提交父罚款,但没有孩子,有没有办法做到这一点?
Hi I am trying to submit parent and children, I am able to submit the Parent fine but not the children, is there any way to do this ?
这是我的code。
@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对象但不是货币的列表
When I submit I get the CurrencyDay object But not the list of Currency
谢谢
sushiBite。
Thanks sushiBite.
推荐答案
我使用的编辑模板,而不是写在循环的意见建议你:
I would recommend you using editor templates instead of writing loops in your views:
<table>
<tr>
<th>Currency</th>
<th>Rate</th>
</tr>
@Html.EditorFor(x => x.Currency)
</table>
,然后在其中将被渲染为货币集合中的每个元素对应的编辑器模板(〜/查看/共享/ 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>
注意,名称和编辑模板的位置是重要的。按照惯例,位置应该是〜/查看/ SomeController / EditorTemplates
或全局以上(如果模板是控制器的动作之间重复使用)〜/查看/共享/ EditorTemplates
。编辑模板的名称应该是你遍历集合的每个元素的类型的名称。
Notice that the name and the location of the editor template is important. By convention the location should be either ~/Views/SomeController/EditorTemplates
(if the template is reused between actions of the controller) or more globally ~/Views/Shared/EditorTemplates
. The name of the editor template should be the name of the type of each element of the collection you are iterating over.
这篇关于Submiting家长和放大器;在剃刀儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!