C# IDictionary MVC中的字典

C# IDictionary MVC中的字典,c#,asp.net-mvc,dictionary,razor,C#,Asp.net Mvc,Dictionary,Razor,我不得不修改一个单选按钮以包含一个新值。 要做到这一点,我必须把字典放在字典里。键对IDictionary很有用,但Dictionary int和string根本不起作用。 我相信这是我的前端代码。 有人能看到我做错了什么,并提供一个如何修复的例子吗 控制器中的动作参数 IDictionary<String, Dictionary<int, String>> IDictionary 查看 <fieldset> <legend class="sr-on

我不得不修改一个单选按钮以包含一个新值。 要做到这一点,我必须把字典放在字典里。键对IDictionary很有用,但Dictionary int和string根本不起作用。 我相信这是我的前端代码。 有人能看到我做错了什么,并提供一个如何修复的例子吗

控制器中的动作参数

IDictionary<String, Dictionary<int, String>>
IDictionary
查看

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

行动
无所事事
@{
var possibleActions=账户余额<0?新[]{
BalanceAdjustmentType.Zeroed,BalanceAdjustmentType.Issuited}
:new[]{BalanceAdjustmentType.Under,BalanceAdjustmentType.Sent};
}
@foreach(可能情况下的var操作)
{
@行动.文本
}

我不明白控制器参数的实际含义。 但我认为您视图中的“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>

行动
无所事事
@{ 
var possibleActions=账户余额<0
?新[]{BalanceAdjustmentType.Zeroed,BalanceAdjustmentType.Issuited}
:new[]{BalanceAdjustmentType.Under,BalanceAdjustmentType.Sent};
} 
@foreach(可能反应中的var作用){
@行动.文本
}

感谢您的回复。当我使用上述方法时,传入控制器的结果是CustomerId作为键,然后该值的计数为0,因此它不会看到嵌套值的YearId或BalanceId。我认为您需要考虑内部键是AccountId或YearId。我不相信这两个可以包含在同一个dict中。请检查上面代码片段中的第6行和foeach循环。