views:

59

answers:

1

Hello, I have a dictionary I'm passing to a View. I want to be able to pass the values of that dictionary back to a Controller action from this same View. Is there anyway this can be accomplished without using a form? The situation is that I need to be able to pass these back to a controller action that is called when a user clicks an ActionLink from the View, and in my experience an ActionLink cannot be used to submit a values of a form, i.e. I've only been able to submit values of a form using the form's submit button. Unless there's a way to use an ActionLink to submit values in a form.

Controller Action passes Dictionary to Controller in ViewData:

public ActionResult ModifyNewCrossListing(FormCollection form)
        {
            Dictionary<int, string> prefixlist = new Dictionary<int, String>();
            ViewDatap["prefixList"] = prefixlist;
        }

View prints out content of Dictionary, ActionLink calls another controller action, which is where I want to access the dictionary:

<%int i = 0;
      if ((ViewData["prefixList"] as Dictionary<int, string>).Count >= 1)
      {
          foreach (var cp in (ViewData["prefixList"] as Dictionary<int, string>))
          {
              if (cp.Value == (ViewData["prefixList"] as Dictionary<int, string>).Last().Value)
              {%>
                  <%= Html.Encode(cp.Value)%>
              <%}
              else
              {%>
                  <%= Html.Encode(cp.Value)%> -
            <%}
          } 
      } %> 

<%=Html.ActionLink("View All Criteria", "ShowAllCriteria", new { prefixList = ViewData["prefixList"] as Dictionary<int, string> })%>)

And finally, in my new controller action, I want to be able to access the dictionary

public ActionResult ShowAllCriteria(Dictionary<int, string> prefixList)
        {
          //Do stuff
        }
+1  A: 

Without a button or form, I would say that your best bet is to hook the link to a Javascript or jQuery method, and post the data back to the controller using AJAX or JSON.

The controller method can then perform the necessary redirect to display a new page.

Robert Harvey
Hi Robert, unfortunately I'm not allowed to use JS. Was thinking, is there a way to pass form values using a ActionLink?? That would fix my problem.
kingrichard2005
Not without Javascript, AFAIK. You are limited to the `Submit` button behavior as it is defined in the browser by the HTML specification for POSTing forms. Without the ability to run script code, there is no way to override the normal behavior of links. In fact, no Javascript puts all sorts of limitations on the behavior of your web pages (you are essentially limited to simple, whole-page GETs and POSTs).
Robert Harvey
Your customer has two choices: they can allow you to use Javascript, or live with buttons. Frankly, the buttons are probably a better choice, as semantically they will have the correct and expected behavior (that of submitting data).
Robert Harvey