读取ViewData子值

时间:2017-10-28 03:03:04

标签: asp.net-mvc-4 viewdata

问:我如何获得此ViewData? @ViewData["MyCustomCollection"]["MyParam1"]它会返回错误Cannot apply indexing with [] to an expression of type 'object'

查看(MyCustomCollecion有[UIHint("MyCustomEditor")]

@Html.EditorFor(model => model.MyCustomCollection, new
{
    MyCustomCollection = new
    {
        Param1 = "MyParam1"
        Param2 = "MyParam2"
    }
})

MyCustomEditor

(...)
<span>@ViewData["MyCustomCollection"]["MyParam1"]<span>
<span>@ViewData["MyCustomCollection"]["MyParam2"]<span>
(...)

重要提示:我需要将MyCustomCollection设为父级。我不能这样做:

@Html.EditorFor(model => model.MyCustomCollection, new
{
   Param1 = "MyParam1"
   Param2 = "MyParam2"
})

1 个答案:

答案 0 :(得分:0)

按照这种方法,我可以做我想做的事。

https://stackoverflow.com/a/47045595/3880331