Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap Multiselect Get Selected values on HttpPost

Tags:

I'm using this Bootstrap Multiselect and my problem is that I cant get the selected values on HttpPost on ASP.Net MVC.

Problems Encountered:

  • after clicking save, Only the first selected value is the present on the model. (SOLVED)

  • after clicking save, Only the first selected value is the present on the dropdownlist.

Sample.chtml:

@model SampleProject.Models.SampleViewModel

@using (Html.BeginForm())
{
    @Html.DropDownListFor(model => model.Selected, new SelectList(Model.List, "value", "text"), new { @class = "multiselect form-control", multiple = "multiple" })
    <input type="submit" value="Save" />
}

Model:

public class SampleViewModel
{
    public string[] Selected { get; set; }
    public SelectList List { get; set; }
}

Controller:

public class DashboardController : Controller
{
    public ActionResult Sample()
    {
        SampleViewModel model = new SampleViewModel();
        model.List = new SelectList(new List<string>() { "1", "2", "3" });

        return View(model);
    }

    [HttpPost]
    public ActionResult Sample(SampleViewModel model)
    {
        model.List = new SelectList(new List<string>() { "1", "2", "3" });

        return View(model);
    }
}

Selection:

Selection

I cant get the selected values correctly on HttpPost

Code Behind/HttpPost: (Wrong) HttpPost

After HttpPost: (Correct)

After