.net mvc pass dictionary data from view to controller
- by Wei Ma
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.