arrkaye
arrkaye

Reputation: 656

Multiple MVC DropDownLists - submitting each form separately

I have an MVC page with multiple DropDownLists. One for each user displayed on the screen.

What I need to do is call an action on the controller when the user changes any drop down lists. This call needs to pass the controller the user which the dropdown corresponds to as well as the value of the selected item.

I have an idea of how to do this with jQuery but not quite sure how to setup the jQuery handler for each DropDown dynamically.

Any ideas?

Sample code:

View:

@foreach (var user in Model.Users)
    {  
        <tr>
            <td>
                    @user.Name
            </td>
            <td>
                @using (Html.BeginForm())
                {
                    @Html.DropDownList(@user.Name + "-dd", @Model.UserGroups, "Add to group...")
                }
            </td>
        </tr>
    }

Controller:

public ActionResult AddToGroup(string user, string selectedValue){....}

Upvotes: 0

Views: 433

Answers (1)

lahsrah
lahsrah

Reputation: 9173

Use a CSS class for the drop down lists

@Html.DropDownList(@user.Name + "-dd", @Model.UserGroups, "Add to group...", new { @class = "user-drop-down" })

Then use JQuery selector

$('.user-drop-down').change(....)  

Put your logic in there for the posting the data to your MVC action. Refer to JQuery APIs for more info on how to implement the javascript method: http://api.jquery.com/change/

Upvotes: 1

Related Questions