1

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" />

    <% } %>
<br/><br/>

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

Edit ( EXTRA CODE )

Controller's action method :

[HttpPost]
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" />

    <% } %>

ERROR:

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.

Regards Usman

1
  • Please post your controller action you need to call once Submit is clicked and the controller that populates your DropDownLists... with that we can see and correct what you are doing ;) Commented Apr 9, 2012 at 0:00

2 Answers 2

1

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:

[HttpPost]
public ActionResult MyAction(string SemesterToOrganize, string SemesterToBaseOn) {
    // your code.
}

EDIT:

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

[HttpPost] 
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.

EDIT2:

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.

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

17 Comments

Actually I have already a filled Html.DropDownList of smesters ( "SS2012,WS2013, SS2011") and in other Html.DroopDownList the same values, Os I just want to get the selected values from both lists and send them as controller action's parameters. That's it!!
@Usman - You're not making any sense. You can't fill your drop down lists until you create them. You creat them with the above code. They automatically post their selected values when you submit the form. What you are describing is impossible given the code you've shown.
@Usman - I suggest you provide the real code, not some made up code, because the code you provide is invalid and doesn't show what you're actually trying to do. Use real code.
This DropDownList is filled in controller's method somehow via some List<string> based list which was filled ( e.g listofSemester.Add("WS2012") sort of...)just forget it. This posts the correct value when only just one arguemnt is being passed on. But I need to work with it via 2 list's selection values and these 2 lists must give their selected values somehow to controller's action method
@Usman - You are asking to forget about something that is critical to solving your problem. If you don't want the advice given, then you won't solve the problem. You're problem is that you're relying on some behavior that won't work in your situation, but you don't want to change it.
|
1
    // Try this. Change names and put in the appropriate namespace. 
    //Your view
    @model MvcApplication2.Models.CloneSemesterDataViewModel 
    @Html.LabelFor(x => x.SemesterToOrganize)
    @Html.DropDownListFor(x => x.SemesterToOrganize, Model.ListofSemestersToOrganize)
    --------
    @Html.LabelFor(x => x.SemesterToBaseOn)
    @Html.DropDownListFor(x => x.SemesterToBaseOn, Model.ListofSemestersToBaseOn)

    //view model
    namespace MvcApplication2.Models
    {
        public class CloneSemesterDataViewModel
        {
            public string SemesterToOrganize { get; set; }
            public IEnumerable<SelectListItem> ListofSemestersToOrganize
            {
                get
                {
                    return new List<SelectListItem> { new SelectListItem { Text = "SS2012" , Value = "SS2012"} };
                }
            }

            public string SemesterToBaseOn { get; set; }
            public IEnumerable<SelectListItem> ListofSemestersToBaseOn
            {
                get
                {
                    return new List<SelectListItem> { new SelectListItem { Text = "SS2012", Value = "SS2012" } };
                }
            }
        }
    }


    ----------
    Controller.

    [HttpPost]
    public ActionResult CloneSemesterData(CloneSemesterDataViewModel viewModel)
    {
       //viewModel.SemesterToBaseOn
       //viewModel.SemesterToOrganize
    }

   // This should do the trick.

2 Comments

The view is written for Razor. I guess its straightforward to convert to aspx.
hi how can i show dropdownlist's selected values into webgrid?

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.