IDictionary MVC中的字典

时间:2017-07-12 14:56:08

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

我不得不改变一个单选按钮以包含一个新值。 要做到这一点,我必须将一个字典放在IDictionary中。密钥对于IDictionary非常有用,但是字典int和字符串根本没有拉入。 我相信这是我的前端代码。 任何人都可以看到我做错了什么,并举例说明如何解决?

控制器中的操作参数

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>

1 个答案:

答案 0 :(得分:0)

我实际上并不了解你的控制器参数意味着什么。 但我认为&#34; accountActions&#34;在您的视图中是您的&#34;控制器参数&#34;的变量。 如果是这样,那么您可以通过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>