【问题标题】:Dictionary inside IDictionary MVCIDictionary MVC 中的字典
【发布时间】:2017-07-12 14:56:08
【问题描述】:

我不得不更改一个单选按钮以包含一个新值。 为此,我必须将 Dictionary 放入 IDictionary 中。键非常适合 IDictionary,但 Dictionary int 和 string 根本不适合。 我相信这是我的前端代码。 谁能看到我做错了什么并提供如何解决的示例?

控制器中的动作参数

IDictionary<String, Dictionary<int, String>>

查看

<fieldset>
<legend class="sr-only">Actions</legend>
<input type="hidden" name="customerId" value="@account.CustomerId" />
<input type="hidden" name="yearSetupId" value="@account.YearId" />
<label class="radio-inline"><input type="radio" name="accountActions[@(account.CustomerId)].Key[@(account.Id)].Value" value="" checked="checked">Do nothing</label>
@{
     var possibleActions = account.Balance < 0 ? new[] { 
     BalanceAdjustmentType.Zeroed, BalanceAdjustmentType.Issued }
                                    : new[] { BalanceAdjustmentType.Under, BalanceAdjustmentType.Sent };
                                }
@foreach (var action in possibleActions)
                                {
<label class="radio-inline"><input type="radio" name="accountActions[@(account.CustomerId)].Key[@(account.YearId)].Value" value="@action.BalanceId">@action.Text</label>
                                }
</fieldset>

【问题讨论】:

    标签: c# asp.net-mvc dictionary razor


    【解决方案1】:

    我不明白你的控制器参数实际上是什么意思。 但我认为您视图中的“accountActions”是您的“控制器参数”的变量。 如果是这样,那么您可以通过 accountActions[Key][InnerKey] 访问嵌套的 dict 值。

    <fieldset>
        <legend class="sr-only">Actions</legend>
        <input type="hidden" name="customerId" value="@account.CustomerId" />
        <input type="hidden" name="yearSetupId" value="@account.YearId" />
        <label class="radio-inline">
            <input type="radio" name="@accountActions[account.CustomerId][account.Id]" value="" checked="checked">
            Do nothing
        </label>    
    
        @{ 
            var possibleActions = account.Balance < 0 
            ? new[] { BalanceAdjustmentType.Zeroed, BalanceAdjustmentType.Issued } 
            : new[] { BalanceAdjustmentType.Under, BalanceAdjustmentType.Sent }; 
        } 
    
        @foreach (var action in possibleActions) { 
            <label class="radio-inline">
                <input type="radio" name="@accountActions[account.CustomerId][account.YearId]" value="@action.BalanceId">
                @action.Text
            </label>
        }
    </fieldset>
    

    【讨论】:

    • 感谢您的回复。当我使用上述方法时,传递给我的控制器的结果是以 CustomerId 作为键,然后该值的计数为 0,因此它看不到嵌套值的 YearId 或 BalanceId。
    • 我认为您需要考虑内部键是 AccountId 或 YearId。我不相信这两个可以包含在同一个字典中。请检查 line:6 和上面代码 sn-p 中的 foeach 循环。
    猜你喜欢
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2012-10-31
    • 2013-02-09
    • 2018-02-05
    • 2023-03-13
    • 2012-11-25
    相关资源
    最近更新 更多