You are using controller, but I'm using x.cshtml.cs.
You specifically tagged this post as dotnet-aspnet-core-mvc and the reason why your getting MVC solutions.
May be because of this I have errors???
Unlikely. Razor Pages is like a wrapper for MVC. Your are receiving error because there are mistakes in your source code or design. This community can help you if you post source code that reproduces the exact error. Otherwise, we can only guess where the mistakes are in your code and/or design. Please post your source code.
Working and tested Index.cshtml.cs example (Razor Pages) .NET 3.1.
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
KeyValuePair<int, string>[] values = new KeyValuePair<int, string>[]
{
new KeyValuePair<int, string>(1, "Hello"),
new KeyValuePair<int, string>(2, "World")
};
ViewData["kvp"] = values;
}
}
Markup
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
var kvp = ViewData["kvp"] as KeyValuePair<int, string>[];
}
<div>
@foreach (var item in kvp)
{
<div>
@item.Key: @item.Value
</div>
}
</div>