0

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
  • Code would help explain your question more clearly. It looks like RedirectToAction() might help in your case. Commented Apr 22, 2010 at 17:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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).
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.