0

The Scenario is where :-

Controller part :

    public ActionResult GoHt()
    {
        Dictionary<string, string> col = new Dictionary<string, string>();
        col.Add("A", "C");
        col.Add("B", "C");
        ViewBag.Cols = col;
        return View();
    }

View Part :

    <input type="hidden" id="HD" name ="HD" value="@ViewBag.Cols" />

In this case hidden value is not showing Dictionary element which id defined instead it is show as

    <input type="hidden" id="HD" name="HD"  value="System.Collections.Generic.Dictionary`2[System.String,System.String]">

Here the Question is , how do i assign Dictionary element to ViewBag and store in Hidden field.

and how do the same Dictionary is made available at Form Submission.

Controller :

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult GoHt(FormCollection formCollection)
    {

        var Mode = (Dictionary<string, string>)formCollection["HD"].ToString();
    }
tereško
  • 58,060
  • 25
  • 98
  • 150
ViZ
  • 81
  • 3
  • 15
  • Does this question answer yours? http://stackoverflow.com/questions/11374097/hidden-input-for-dictionarystring-string-in-asp-net-mvc-3 – FantasticJamieBurns Apr 14 '14 at 14:20
  • Or perhaps this: http://stackoverflow.com/questions/5191303/asp-net-mvc-binding-to-a-dictionary – FantasticJamieBurns Apr 14 '14 at 14:21
  • Not. Actually here i want similar functionality as ViewState[""] of web forms to store any kind of data and retrieve the same. – ViZ Apr 15 '14 at 03:21
  • For a dictionary either you access to the value or the key to save it in a hidden field. if you save the object you will have the result above. the name of the object. What I can suggest you to have maybe two hidden field and one you save the key and the other you save the value –  Apr 15 '14 at 06:01
  • How to save the Dictionary in Hidden field in view and retrieve the same in FormCollection frm object. – ViZ Apr 15 '14 at 06:08

2 Answers2

2

You will need multiple hidden fields, for each key in the dictionary. Scott Hanselman wrote a nice blog post explaining how your hidden fields should be named. This way you will be able to retrieve the values as a strongly typed Dictionary object in your controller.

<input type="text" name="col[0].Key" value="A" />
<input type="text" name="col[0].Value" value="C" />
<input type="text" name="col[1].Key" value="B" />
<input type="text" name="col[1].Value" value="C" />
...

Now your controller action could directly take the dictionary as parameter:

[HttpPost]
public ActionResult GoHt(Dictionary<string, string> col)
{
    // do something with col here ...
}

So now all that's left for you is to loop through the values of the dictionary and generate those hidden fields:

@foreach (var item in (Dictionary<string, string>)ViewBag.Cols)
{
    @Html.Hidden("cols.Key", item.Key)
    @Html.Hidden("cols.Value", item.Value)
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • how do i get (Dictionary col) in Controller part in HttpPost . – ViZ Apr 15 '14 at 06:21
  • It is passed as parameter to your controller action. The default model binder will populate the values as long as you have your hidden fields correctly named. Look at my answer more carefully and the signature of the HttpPost action. – Darin Dimitrov Apr 15 '14 at 07:13
0

In the Controller part You can write the same code.

     public ActionResult GoHt()
     {
     Dictionary<string, string> col = new Dictionary<string, string>();
     col.Add("A", "C");
     col.Add("B", "C");
     ViewBag.Cols = col;
     return View();
     }

In the View page you need to reassign your viewbag data to Temp data like this

    @{

    TempData["Dict"] = ViewBag.Cols; 
    }

Now on the Form Submission you can retrieve the dictionary values like this

    public ActionResult GoHt(FormCollection formCollection)
    {
    Dictionary<string, string> obj = (Dictionary<string, string>)TempData["Dict"];
    }
  • 1
    using TempData the Data is lost in second redirect..and so how it will be unique for all pages in browser. – ViZ Apr 15 '14 at 07:02
  • In that case you can use Session instead of TempData, that will be accessible even after multiple redirect. –  Apr 15 '14 at 07:47