What type of objects can be sent back to an action Method using HTML.HIDDEN()

Posted by Richard77 on Stack Overflow See other posts from Stack Overflow or by Richard77
Published on 2010-04-14T10:35:59Z Indexed on 2010/04/14 14:53 UTC
Read the original article Hit count: 167

Filed under:
|

Hello,

1)Let's say I've this form:

<%Using(Html.BeginForm()){%>
 <% = Html.Hidden("myObject", 
                   (cast to the appropriate type)ViewData["KeyForMyObject"]%>     
 <input type = "submit" "Submit Object">
<%}%>

2) Here's the Action which's supposed to intercept the value of the object

public ActionResult MyAction(Type myObject)
{
  //Do Something with the object 
}

Here's my question: What type of objects the Hidden field can support?

In fact, when ViewData["KeyForMyObject"] contains a string, int, or bool, myAction is able to retrieve the value.

But, when it comes to objects, such as List, and dictionary, nothing happens. When I debug to check the local values, I see null for Type myObject in the MyMethod.

So what are the rules in MVC when it comes to a List or Dictionary?

================================= EDIT

To make things simpler, can I write something like this

<% = Html.Hidden("contactDic", (Dictionary<string, string>) ViewData["contacts"])%>

and expect to retrieve the dictionary in the action Method like this

public ActionResult myMethod(Dictionary<string, string> contactDic)
{
  //Do something with the dictionary
}

Thanks for Helping

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about XHTML