【发布时间】:2018-02-27 00:30:36
【问题描述】:
我有一个 asp.net 核心输入表单,它需要遍历具有键类型“产品”和值类型“int”的字典(其中 int 应该是产品的数量)。现在看起来像这样:
@foreach (KeyValuePair<Product, int> product in Model.ProductQuantityDictionary)
{
<tr>
<td>
@product.Key.Name
</td>
<td>
<input asp-for="@product.Value" class="form-control" />
</td>
</tr>
}
这成功地显示了产品名称和具有正确默认值 (0) 的输入字段,但是当我提交它时,它实际上并没有提交字典条目。我认为这是因为我只将值而不是 Key 或总 KeyValuePair 输入到字典中,但我不知道该怎么做。我需要为每对现有的键和输入的值做一些事情,更新字典中的条目(字典已经有它需要的所有条目数,我在这里所做的只是更新值)
感谢您的帮助
编辑:这是视图模型。我需要填写该字典,然后将其值传回并根据每对中选择的数量制作产品实例
public class ProductKit_ViewModel
{
public int Id { get; set; }
public int EventId { get; set; }
public string Name { get; set; }
public int StyleId { get; set; }
public Dictionary<Product, int> ProductQuantityDictionary { get; set; }
}
【问题讨论】:
标签: c# asp.net-core asp.net-core-tag-helpers