0

So in my view I am dynamically creating dropdown lists in a for loop. I use the same name value in unique id values.

@foreach(var item in Model.Items)
{
   @Html.DropDownList("Items" Model.GenerateSelectList(item.id), new { id = item.id })
}

In my controller action method for the post I can get the values of the dropdowns like this:

[HttpPost]
[ValidateAntiForgeryToken]
[Authorize]
public ActionResult ClassicLineup(IList<int> items)
{
}

I cannot figure out how to get BOTH the dropdown id and associated value.

Seems like it should be simple but it has me stumped...

Thanks.

2
  • are you interested in getting only selected item?? Commented Aug 6, 2011 at 5:00
  • correct I only care about getting the selected value and the corresponding dropdown id Commented Aug 6, 2011 at 16:01

2 Answers 2

2

On form submit browser sends only the selected value of a dropdown, so there is no bult in mechanism to read the text of selected item, but you can create one for you.

In below code i have created a hidden input which stores the text of current selected item, and is send to the server on form post.

You can create below list in controller or in view (preferred place in controller);

var items= (from item in Model.Items
                select new SelectListItem
                {
                    Value= item.DisplayProperty
                    Text= item.Value
                }).toList();


 <form  action="controller/test">

 @*This will create DD*@ 
 @Html.DropDownList("MyDropDownList", items)

 @*Hidden input*@ 
<input type="hidden" id="ddSelectedName" name="ddSelectedName" />

 <br>
     <input type="submit" value="submit"/>
   </form>

Include jQuery in ur code and then add this

<script type="text/javascript">
    $('#MyDropDownList').change(function () {
        $('#ddSelectedName').val($(this).find(':selected').text());
    });
</script>

Controller

public string test(string MyDropDownList, string ddSelectedName)
{
    return MyDropDownList+ "--"+ddSelectedName ;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am not trying to get the text value of the dropdown. Just the selected dropdown values and there associated html select id value. Making it IList<int> for the paramter action method gets me all the values, but I do not have the id of the dropdown there are associated with.
the test controller i have mentioned gets you both id & text of selected value!!
0

I just ended up looping through the forms collection and parsing the dropdown id (form.key) and the dropdown value (form.key.value).

Apparently there is not an easy way to have MVC bind the dropdown values and id to a collection in the controller action parameter.

Comments

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.