Search code examples
asp.net-mvc-4viewdata

Read ViewData Sub Values


Q: How can I get this ViewData? @ViewData["MyCustomCollection"]["MyParam1"] it returns the errorCannot apply indexing with [] to an expression of type 'object'

View (MyCustomCollecion has [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>
(...)

Important: I need to set MyCustomCollection as a parent. I cannot do this:

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

Solution

  • It's possible to do what I was trying to, following this approach.

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