简体   繁体   English

DB填充了EditorTemplate中的DropDownList

[英]DB filled DropDownList in EditorTemplate

I have an import page with a dynamic number of transactions. 我有一个包含动态交易数量的导入页面。 For each transactions, I have several plain text data labels and one DropDownList (Categories). 对于每个事务,我有几个纯文本数据标签和一个DropDownList(Categories)。 I'm trying to populate this Category DropDownList with data from my ViewModel ( Categories ) by passing the Categories Model as additionalViewData to my EditorTemplate. 我试图通过将Categories Model作为additionalViewData传递给我的EditorTemplate来使用来自我的ViewModel( Categories )的数据填充此Category DropDownList。

When I use the example below, I get the following error on the EditorTemplate page: Compiler Error Message: CS0411: The type arguments for method 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' cannot be inferred from the usage. 当我使用下面的示例时,我在EditorTemplate页面上收到以下错误:编译器错误消息:CS0411:方法'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper,System)的类型参数.Linq.Expressions.Expression>,System.Collections.Generic.IEnumerable)'无法从使用中推断出来。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

Any ideas on how to fix this? 有想法该怎么解决这个吗?

ViewModel: 视图模型:

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

Model: 模型:

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

Form: 形成:

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

You could provide the categories as additionalViewData to the Editor, like 您可以将类别作为additionalViewData提供给编辑器,例如

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

but be aware of the implication, that you have to do that everywhere where you use that editor. 但请注意其含义,即在使用该编辑器的任何地方都必须这样做。

It might be better to use Editor where you can pass the categories along with the Model expression 使用编辑器可能更好,您可以将类别与模型表达式一起传递

It is not too Strongly typed, but in your controller you can populate the list of categories and place it in the ViewBag. 它的类型不是太强,但在控制器中,您可以填充类别列表并将其放在ViewBag中。 Then you can use it in every Transaction you could have, and it is stored in memory just once. 然后,您可以在每个可能的事务中使用它,并且它只存储在内存中一次。

If you place the Categories collection in each Transaction in your list you can have it multiple times. 如果将Categories集合放在列表中的每个Transaction中,则可以多次使用它。

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

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