I’m working on an ASP.NET MVC3 app, and I’d like to make a call from the view to the controller and store that information as a Dictionary.
Controller code:
public Dictionary<string,int> foo()
{
Dictionary<string,int> bar = new Dictionary<string,int>();
bar.Add("test",100);
return bar;
}
View code:
@{ Dictionary<string,int> foobar = Html.Action("foo"); }
...
<div>@foobar["test"]</div>
I can get the view to work if I use var foobar = Html.Action("foo");, but then it just says that foobar is of type System.Web.Mvc.MvcHtmlString, so I can’t do much with it.
Is there any built-in functionality that I’m missing, or should I just use something like a JSON result for this?
EDIT: Additional Info
It should be noted also, that in the VS2010 debugger, it recognizes foobar properly as {System.Collections.Generic.Dictionary'2[System.String,System.Int32]}, so the issue is that foobar isn’t resolving correctly. The error thrown is:
Compiler Error Message: CS0021: Cannot apply indexing with []
to an expression of type 'System.Web.Mvc.MvcHtmlString'
EDIT 2
These are the errors that came with casting the result to a dictionary:
Url.Action("foo").ToDictionary<string,int>(); returned CS1501: No overload for method 'ToDictionary' takes 0 arguments for obvious reasons, but the VS2010 debugger recognizes that the result is a Dictionary:

But when I add parameters (Url.Action("foo").ToDictionary<string,int>(x=>x);), it stops recognizing that it is a dictionary, although I’m not sure those are the proper params (it’s based off what I found here.

You should try as much as possible to keep views dumb. The controller has to push the data to the view. For some reason if you want to perform call a function that performs complex calculations then a better idea would be create a separate static class and move the function from controller to that class. The
Html.Actionis for entirely a different purpose. Your controllers should always return action results not dictionaries or other types.