1

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Richard77
  • 20,343
  • 46
  • 150
  • 252

2 Answers2

0

The notation for lists is a bit more complex.

Something like [0].Title if you want the first item Title

See http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx.

Mathias F
  • 15,906
  • 22
  • 89
  • 159
0

The present question is one of many I've asked. They all have one thing in common: I was trying to keep the state of my data (Multisteps/Wizard like application). So I was trying to use statement such as ModelState.Clear() abusively to clear the content of the ViewData.

Now I realize that I was fighting the way ViewData is supposed to work (e.i. MVC itself). To conclude, to keep the state of my objects, I need to serialize/deserialize object.

The best sample I found is the Multisteps Wizard p.396-406 "Pro ASP.NET MVC/Steve Sanderson/Apress"

Also see how to stimulate a ViewState.

It looks like I just found out what I was looking for without knowing how to express myself.

Thanks

Community
  • 1
  • 1
Richard77
  • 20,343
  • 46
  • 150
  • 252