.net mvc pass dictionary data from view to controller

Posted by Wei Ma on Stack Overflow See other posts from Stack Overflow or by Wei Ma
Published on 2009-11-30T12:56:19Z Indexed on 2010/04/02 10:33 UTC
Read the original article Hit count: 367

Filed under:
|

A while ago, I was trying to pass a dictionary data from my view to my controller. And I was able to do so after googling on the net(remember it was one of scott hanselman's posts). The solution I had was something like

<%for(int index=0; index<Model.Count(); index++){
     var property= Model.ElementAt(index);%>
     <input type="hidden" name="<%="properties["+index+"].Key"%>"/>
     <input type="hidden" name="<%="properties["+index+"].Value"%>"/>
<%}%>

public ActionResult Process(IDictionary<string,string> properties)
{
      doSomething();
      return View();
}

The code worked for awhile and then I did some refactoring and got rid of this chunk of code. Today, I ran into a situation in which I would like to pass a dictionary again. But no matter how hard I try, the properties parameter received by the action was always null. I tried the above code and

<%for(int index=0; index<Model.Count(); index++){
     var property= Model.ElementAt(index);%>
     <input type="hidden" name="<%="properties.Keys["+index+"]"%>"/>
     <input type="hidden" name="<%="properties.Values["+index+"]"%>"/>
<%}%>

Neither code worked. I googled again but couldn't find the post that helped me before. Can someone point out what I did wrong? thanks a million.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about asp.net-mvc