@Html.ListBoxFor usage and creation questions

user1599647 picture user1599647 · Aug 22, 2012 · Viewed 40.1k times · Source

I have a webpage with 2 list box (list1, list2), I can shift items from one listbox to the other and back. When I click on the submit button, I want to return all the items value in list2 to the httppost method.

I have 3 problems:

  1. How to definite the property for the selected list?
  2. How to have a initial empty list (for list2)? list1 has populated items?
  3. How to return all of list2 items to model, when submit button is clicked ?

Model:

public class TestModel
{
    public List<SelectListItem> OptionList { get; set; }
    public List<SelectListItem> SelectedOptionList { get; set; }
    public string[] SelectedOptions { get; set; }
}

Controller:

private TestModel testModel  = new TestModel();

public ActionResult TestPage()
{
    ViewBag.Message = "Test Page";

    testModel.OptionList = new List<SelectListItem>();

    for (int i = 1; i <= 100; i++)
    {
        SelectListItem item = new SelectListItem();
        item.Text = "Option " + i.ToString();
        item.Value = item.Text;

        testModel.OptionList.Add(item);
    }

    return View("TestView", testModel);
}

[HttpPost]
public ActionResult TestPage(TestModel formModel)
{
    testModel.SelectedOptionList = formModel.SelectedOptionList;
    testModel.SelectedOptions = formModel.SelectedOptions;

    //do something
    return RedirectToAction("TestPage");
}

View:

@model EngineeringDataAnalysisGui.Models.TestModel

@{
    ViewBag.Title = @ViewBag.Message;
}

@section Subtitle
{
    @ViewBag.Message
}

<table width="100%">
@using (Html.BeginForm("TestPage", "RetrieveData", FormMethod.Post))
{
    <tr>
        <td>Event List:</td>
        <td>@Html.ListBox("lstEventSelection", Model.OptionList, new { id = "list1", Multiple = "multiple", Size = 15, style = "width: 100%;" })</td>
    </tr>
    <tr>
        <td></td>
        <td>
            <table style="text-align:center; width: 100%">
                <tr>
                    <td id="buttonCol">
                        <input type="button" id="btnAddAllEvent" title="Add All Events" onclick="moveAllOptions('list1','list2', true)" />
                        <input type="button" id="btnAddEvent" title="Add Selected Events" onclick="moveSelectedOptions('list1','list2', true)" />
                    </td>
                    <td id="buttonCol">
                        <input type="button" id="btnRemoveEvent" title="Remove Selected Events" onclick="moveSelectedOptions('list2','list1', true)" />
                        <input type="button" id="btnRemoveAllEvent" title="Remove All Events" onclick="moveAllOptions('list2','list1', true)" />
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>Selected Event List:</td>
        @*Not sure how to add the listbox*@
        <td>@*Wrong*@@Html.ListBoxFor(x => x.SelectedOptions, Model.SelectedOptionList, new { id = "list2", Multiple = "multiple", Size = 15, style = "width: 100%;" })</td>
    </tr>
    <tr>
        <td><input type="submit" value="submit" /></td>
    </tr>
}
</table>

Answer

aravind picture aravind · Aug 22, 2012

1) How to definite the property for the selected list?
@Html.ListBoxFor(x => x.SelectedOptions, new MultiSelectList(Model.SelectedOptionList), new { id = "list2", Multiple = "multiple", Size = 15, style = "width: 100%;" })

2) How to have a initial empty list (for list2)? list1 has populated items. In controller set an empty list testModel.SelectedOptionList = new List<SelectListItem>();

3) How to return all of list2 items to model when submit button is clicked
Probably you need to set all the items in list2 as selected (using jquery) before submitting. All the selected items in list2 items will bind to TestModel.SelectedOptions on submit.

Please look around, there are a lot of answers already in SO related to Html.ListBoxFor. for e.g Challenges with selecting values in ListBoxFor