繁体   English   中英

DB填充了EditorTemplate中的DropDownList

[英]DB filled DropDownList in EditorTemplate

我有一个包含动态交易数量的导入页面。 对于每个事务,我有几个纯文本数据标签和一个DropDownList(Categories)。 我试图通过将Categories Model作为additionalViewData传递给我的EditorTemplate来使用来自我的ViewModel( Categories )的数据填充此Category DropDownList。

当我使用下面的示例时,我在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" />
}

EditorTemplate:

<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>

您可以将类别作为additionalViewData提供给编辑器,例如

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

但请注意其含义,即在使用该编辑器的任何地方都必须这样做。

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

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

如果将Categories集合放在列表中的每个Transaction中,则可以多次使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM