Vardens Archangel
Vardens Archangel

Reputation: 375

ASP MVC 6: sending multiselectselect as POST

I have a view in my app, that allows user to set up 2 multiselects. The problem is: how can I send contents of those multiselects back to controller. So far I have:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <div id="member-list">
        <div class="row" id="transfer">
            <div class="col-md-6">
                <select multiple="multiple" id="right-select">
                    @foreach (var item in Model.AnotherContacts)
                    {
                        <option value="@item.Id" onclick="transfer(this)">
                            @item.Email - @item.Name
                        </option>
                    }
                </select>

            </div>
            <div class="col-md-6">
                <select multiple="multiple" id="left-select">
                    @foreach (var item in Model.Group.Members)
                    {
                        <option value="@item.Contact.Id" onclick="transfer(this)">
                            @item.Contact.Email -  @item.Contact.Name
                        </option>
                    }
                </select>
            </div>
        </div>
    </div>
        <div class="col-md-3">
            <input type="submit" class="btn btn-grey" value="Save" />
        </div>

    </div>

And now: how to write a controller to have there list of items, hat I have in both multiselects?

    [HttpPost]
    public ActionResult Manage(int id)
    {


        return View();
    }

Upvotes: 0

Views: 34

Answers (2)

ertdiddy
ertdiddy

Reputation: 407

Well first for a form post to be successful you need to add name attributes to your inputs.

<select multiple="multiple" id="left-select" name="leftSelect">
<select multiple="multiple" id="right-select" name="rightSelect">

Then your controller would be

[HttpPost]
public ActionResult Manage(int[] leftSelect, int[] rightSelect)
{
    //your code

    return View();
}

Notice the names of method parameters are the same as the name attributes on the select lists.

Note: I assumed your @item.Contact.Id and @item.Id are of type int.

Upvotes: 1

Mikael Puusaari
Mikael Puusaari

Reputation: 1044

Make your controller expect several parameters

[HttpPost]
public ActionResult Manage(int id, int id2)
{
    //your code

    return View();
}

This of course depends on what you want to send to to the controller, but I hope you get the idea. You havent shown the code required, but I would guess you have selectLists that return strings, and if that is the case, change the parameters in the Manage Action to expect string variables

Upvotes: 0

Related Questions