2016-11-16 87 views
0

在我看来,使用字典:与ASP.NET MVC4

<tbody> 
    @foreach (var item in Model) 
    { 

     foreach (KeyValuePair<string, Dictionary<string, int>> fs in item.dicFS) 
     { 
      <tr> 
       <td>@Html.DisplayFor(modelItem => fs.Key.ToString())</td> 

       @foreach (KeyValuePair<string, int> lbl in item.dicFS[fs.ToString()]) 
       { 
        <td>@Html.DisplayFor(modelItem => lbl.Value.ToString())</td> 
       } 

      </tr> 
     } 
    } 
</tbody> 

错误:

An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code 

Additional information: Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions. 

这是该行强调,当应用程序崩溃

<td>@Html.DisplayFor(modelItem => fs.Key.ToString())</td> 

我的模型包含了包含字符串键和字典值的公共字典(嵌套字典)

public Dictionary<string, Dictionary<string, int>> dicFS; 

的键和嵌套字典被初始化,并且值是通过在模型内的方法进行分配。

不允许我使用字典?我必须将模型的数据类型转换为嵌套数组,而不是使用嵌套字典吗?

在此先感谢您的帮助!

回答

1

不能使用.ToString()DisplayFor()方法。

只需卸下的ToString(),你应该是好的

<tbody> 
    @foreach (var item in Model) 
    { 

     foreach (KeyValuePair<string, Dictionary<string, int>> fs in item.dicFS) 
     { 
      <tr> 
       <td>@Html.DisplayFor(modelItem => fs.Key)</td> 

       @foreach (KeyValuePair<string, int> lbl in item.dicFS[fs.ToString()]) 
       { 
        <td>@Html.DisplayFor(modelItem => lbl.Value)</td> 
       } 

      </tr> 
     } 
    } 
</tbody> 
+0

嗯,我会被该死的......我期待一些复杂的分辨率。我不知道。谢谢! – blacksaibot