0

我有一个包含动态交易数量的导入页面。对于每笔交易,我都有几个纯文本数据标签和一个 DropDownList(类别)。我试图通过将类别模型作为附加视图数据传递给我的 EditorTemplate,Category用我的 ViewModel () 中的数据填充这个 DropDownList 。Categories

当我使用下面的示例时,我在 EditorTemplate 页面上收到以下错误:编译器错误消息:CS0411:方法的类型参数 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System .Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' 无法从用法中推断出来。尝试明确指定类型参数。

有想法该怎么解决这个吗?

视图模型:

public class ImportViewModel
{
    public List<AbnAmroTransaction> AbnAmroTransactions { get; set; }
    public IEnumerable<SelectListItem> Categories { get; set; }
}

模型:

public class AbnAmroTransaction
{
    public string Currency { get; set; }
    public int DateTime { get; set; }
    public string Description { get; set; }
    public int CategoryId { get; set; }
}

形式:

@using (Html.BeginForm("ImportPreview", "Home", FormMethod.Post))
{
    <table>
    @Html.EditorFor(m => m.AbnAmroTransactions, new { Categories = Model.Categories });
    </table>
    <input id="btnSave" type="submit" value="Save data" />
}

编辑器模板:

<tr>
    <td style="width: 80px;">
        @Html.Raw(CurrencyHelper.GetHtmlCurrency(Model.Currency, Model.Amount))
    </td>
    <td>@Model.DateTime</td>
    <td>@Model.Description</td>
    <td>@Html.DropDownListFor(Model.CategoryId, ViewData["Categories"])</td>
</tr>
4

2 回答 2

2

您可以将类别作为附加视图数据提供给编辑器,例如

@Html.EditorFor(m => m.AbnAmroTransactions, {Categories = Model.Categories});

但请注意这意味着您必须在使用该编辑器的任何地方都这样做。

使用编辑器可能会更好,您可以在其中将类别与模型表达式一起传递

于 2012-05-20T17:19:25.123 回答
0

它的类型不是太强,但是在您的控制器中,您可以填充类别列表并将其放置在 ViewBag 中。然后你可以在你可能拥有的每个事务中使用它,并且它只存储在内存中一次。

如果您将类别集合放在列表中的每个事务中,您可以多次使用它。

于 2012-05-20T17:33:19.893 回答