How to declare in the View?

S A 81 Reputation points
2022-04-01T12:31:43.653+00:00

How to declare in the View?
@{
var kvp = ViewData["kvp"] as KeyValuePair<int, string>[]>;
}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,178 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2022-04-01T14:28:03.343+00:00

    Your KeyValuePair syntax is incorrect so I'm not sure of the exact code. The cast syntax is either...

    @{  
        var kvp = (KeyValuePair<int, string>)ViewData["kvp"];  
    }  
    

    ...or...

    @{  
        var kvp = (KeyValuePair<int, string[]>)ViewData["kvp"];  
    }  
    

    Casting and type conversions (C# Programming Guide)


6 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,526 Reputation points
    2022-04-01T15:24:58.903+00:00

    try (assuming it is an array of KeyValuePairs, often its an IEnumable instead) :

    @{ 
        var kvp = ViewData["kvp"] as KeyValuePair<int, string>[]; 
    }
    

  2. AgaveJoe 26,136 Reputation points
    2022-04-01T15:28:25.25+00:00

    There is an error with your both suggestions: "'KeyValuePair' is 'type' and not valid in the given context."

    Keep in mind it is very difficult to provide an accurate solution when you post code with compiler errors. I have no idea what your intention is.

    Anyway, from your response it seems you are passing a collection of key value pairs.

    @{
        var kvp = ViewData["kvp"] as KeyValuePair<int, string>[];
    }
    

    Or

    @{
        var kvp = (KeyValuePair<int, string>[])ViewData["kvp"];
    }
    

    I typically take advantage of List<T>.

    @{
        var kvp = (List<KeyValuePair<int, string>>)ViewData["kvp"];
    }
    

  3. AgaveJoe 26,136 Reputation points
    2022-04-01T19:24:08.47+00:00

    Dear @AgaveJoe , unfortunately, all your suggestions are unrecognizable ! Please don't reply without testing it !

    I did test code before posting. Most likely there are other issues with your code and design that we cannot see. You only showed us one line of code and that line does not even compile. I'm sure there many other hidden gems in your source code.

    [HttpGet]  
    public IActionResult Index()  
    {  
        KeyValuePair<int, string>[] values = new KeyValuePair<int, string>[]  
        {  
            new KeyValuePair<int, string>(1, "Hello"),  
            new KeyValuePair<int, string>(2, "World")  
        };  
      
        ViewData["kvp"] = values;  
        return View();  
    }  
    

    View

    @{  
        ViewData["Title"] = "Index";  
        var kvp = (KeyValuePair<int, string>[])ViewData["kvp"];  
    }  
      
    <h1>Index</h1>  
      
    <div>  
        @foreach (var item in kvp)  
        {  
            <div>  
                @item.Key: @item.Value  
            </div>  
        }  
    </div>  
    

  4. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2022-04-04T01:41:22.407+00:00

    Hi @S A ,

    var kvp = ViewData["kvp"] as KeyValuePair<int, string>[]>;    
    

    The above code is correct, please check this result:

    189486-image.png

    When loop through the data, it is better to check whether the data is null or not. More detail information about the view, you can refer the following tutorial:

    Views in ASP.NET Core MVC

    Razor syntax reference for ASP.NET Core

    At the end, if I misunderstand about your problem, please feel free let me know and share the detailed question and code and the error message.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion