We help IT Professionals succeed at work.
Get Started

How to pass an array variable from View to Controller?

lapucca asked
Last Modified: 2013-11-08
Basically, I need to store an array of strings and the array index could be the key.  The elements are added each time the Add button is clicked and a jQuery click event code would add items to the array.  When the form is submitted then the array data need to be passed to the Controller Action.  How can I implement this?  Thank you.  
<input type="button" id="addResearchInterest" ------- is the button that would trigger the jQuery code to add an item to the array each time the button is clicked.
<% using(Html.BeginForm("Search", "SearchMembers")) { %>
    <input type="hidden" name="ResearchInterests" id="researchInterests" value=""/>
<%--    <%= Html.Hidden("interestsPathNId", "", new Dictionary<string, object>()%>
--%>   <%-- <%= Html.Hidden("fullInterestsPath", "", new{@class = "myClass"}) %>--%>

        <h1>Search CTSI Members</h1>
        <br class="clear" />
        <label>Areas of Research Interest</label>
        <select id="level1ResearchInterests" name="Level1ResearchInterests" size="5" tabindex="1">
                if (ViewData["Level1ResearchInterests"] != null)
                    foreach (ResearchInterest researchInterest in ViewData["Level1ResearchInterests"] as IList<ResearchInterest>)
                    { %>
                <option value="<%=researchInterest.Id%>"><%=researchInterest.Name%></option>
            <% }
        <select id="level2ResearchInterests" name="Level2ResearchInterests" size="5">
        <select id="level3ResearchInterests" name="Level3ResearchInterests" size="5">
        <select id="level4ResearchInterests" name="Level4ResearchInterests" size="5">
        </select><br /><br />
        <div class="buttonWrapper">
            <input type="button" id="addResearchInterest" value="Add Research Interest to my search criteria(s)" />
        <br />
        <table id='selectedResearchInterests' class="expandingTable">
                    <th>My selected Research Interest</th>
                <tr class="odd">
                    <td>No Research Interests selected yet.</td>
    <div class="buttonWrapper">
        <input type="submit" id="searchMembers" value="Search" tabindex="2" />
<% } %>

Open in new window

Watch Question
This problem has been solved!
Unlock 2 Answers and 18 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE