– How to pass multiple Html.DropDownList selected values from View( .aspx ) to MVC controller’s action

I need to pass multiple data ( probably 2 Html.DropDownList's selected values ) to MVC controller action method from MVC View ( .aspx). I think it would be from somehow Html.Hidden form , but how?

I am unable to get the selected value from Html.DropDownList and pass it as Html.Hidden("paramName", MvcStringSelectedValue) to controller's action.

My Code is :

        based on<br />

        <%: Html.DropDownList("Semester")%>
        <%= Html.Hidden("strSemesterToBaseOn",returnedValueFromAbove)%>

        <%: Html.ValidationSummary(true) %>           
        <input type="submit" value="Clone" />

    <% } %>

Do I need to write the input tag of "submitt" 2 times or just only once?


Controller's action method :

public ActionResult CloneSemesterData(string strSemesterToOrganize, string strSemesterToBaseOn)


HERE ( another Controller's method ) IS THE DROP DOWN LIST Filled with Semester values

   public ActionResult DepartmentAdministration()
      // Get list of semesters
      var lr = new ListRepository();
      ViewData["Semester"] = new SelectList(lr.ListSemester(3));  //this ListSemester(3) will generate the list with 3 strings ( e.g "WS 2012", "SS2010")

      return View();

My View code in .aspx file is :

//this executes when radioButton ="Clone" is selected

    <% using (Html.BeginForm("CloneSemesterData", "CourseNeededHours")) 
        <%= Html.DropDownList("Semester")%> // this is First drop down list box , from which selected value , I want to transfer as 1st parameter of controller's action method

        <%: Html.ValidationSummary(true) %>           

        based On 

        <%= Html.DropDownList("Semester")%> //this is Second drop down list box, from which selected value, I want to transfer as 2nd parameter of controller's action method.
          <input type="submit" value="Clone" />

    <% } %>


Now, after fixing using Edit 2 : it is giving red lines under

as it is somehow not recognizing the ViewData["SemesterList"]…

"System.Web.Mvc.HtmlHelper does not contain a definition for 'DropDownList' and the best extension method overloaded 'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string,System.Collections.Generic.IEnumerable') has some invalid arguments".

Hope now it will clear, still ambiguity , do let me know then.


Best Solution

I am not really sure what you're asking here. You don't need any kind of hidden field to post the selected values of a dropdown. Your Dropdownlist code is invalid to begin with.

Typically you have something like this:

<%= Html.DropDownList("SemesterToOrganize", GetSemesterToOrganize()) %>
<%= Html.DropDownList("SemesterToBaseOn", GetSemesterToBaseOn()) %>

And in your controller:

public ActionResult MyAction(string SemesterToOrganize, string SemesterToBaseOn) {
    // your code.


Based on what you've told us. You are relying on the behavior of MVC of populating the DropDownList because you are adding your list to the ViewData with the same name as your dropdownlist. This won't work for you. You will have to populate each dropdown list seperately.

In your controller, do something like this:

public ActionResult MyAction ()
    ViewData["SemesterList"] = // list of semesters

    return View();

Then, in your view you have:

<%= Html.DropDownList("SemesterToOrganize", ViewData["SemesterList"]) %> 
<%= Html.DropDownList("SemesterToBaseOn", ViewData["SemesterList"]) %> 

then your post method

public ActionResult MyAction(string SemesterToOrganize, string SemesterToBaseOn) { 
    // your code. 

If you want to continue to argue that you can do it your way, then you won't solve your problem. Each dropdown must have it's own unique id, otherwise it will not post correctly. The only way to solve this problem is to give each it's own unique id. That breaks the behavior of the drop down automatically getting the data, so you MUST specify the list of data explicitly.

So stop arguing that this is an unimportant part of the problem. It's not. It's key to the problem.


Based on your code above:

<%= Html.DropDownList("strSemesterToOrganize", (SelectList)ViewData["Semester"]) %>
<%= Html.DropDownList("strSemesterToBaseOn", (SelectList)ViewData["Semester"]) %>

That's all you need

If you had just given us this, and didn't argue, this would been solved a lot easier.

Related Question