jQuery MultiSelect dropdownlist how to access results?

后端 未结 3 961
陌清茗
陌清茗 2021-01-07 01:06

How can I get the results from a JQUery Multi-select dropdownlist as called per an mvc3/razor?

http://abeautifulsite.net/blog/2008/04/jquery-multiselect/

相关标签:
3条回答
  • 2021-01-07 01:47

    Please try this to get selected values

    public ActionResult Index(MyViewModel model, FormCollection collection)
        {
            string selectedValues=collection["SelectedValues[]"]; //here you get comma separated values
            return View(model);
        }
    
    0 讨论(0)
  • 2021-01-07 01:49

    The multiselect plugin uses the [] notation to send the selected values to the server. As always we start by writing a view model:

    public class MyViewModel
    {
        public IEnumerable<string> SelectedValues { get; set; }
    
        public IEnumerable<SelectListItem> Items
        {
            get
            {
                return new[]
                {
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" },
                    new SelectListItem { Value = "3", Text = "item 3" },
                };
            }
        }
    }
    

    then a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    a corresponding view:

    @model MyViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.multiSelect.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#SelectedValues").multiSelect();
        });
    </script>
    
    @using (Html.BeginForm())
    {
        @Html.ListBoxFor(x => x.SelectedValues, Model.Items)
        <button type="submit">OK</button>
    }
    

    and finally a model binder associated with the IEnumerable<string> type and which will work with the [] notation used by the plugin:

    public class MultiSelectModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = (MyViewModel)base.BindModel(controllerContext, bindingContext);
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "[]");
            if (value != null)
            {
                return value.RawValue;
            }
            return model;
        }
    }
    

    The last part is to register the model binder in Application_Start:

    ModelBinders.Binders.Add(typeof(IEnumerable<string>), new MultiSelectModelBinder());
    
    0 讨论(0)
  • 2021-01-07 01:54

    Actually, you can also recieve the selected values by adding a parameter to your action.

    For instance, lets say you have following listbox:

    //Controller GET
    public ActionResult ManageUsers()
    {
        ViewBag.Users = new SelectList(repository.GetAllUsers(), "Id", "Email");
        return View();
    }
    
    //View
    @Html.ListBox(ViewBag.Users as SelectList, new { @id = "users" })
    
    //Controller POST
    [HttpPost]
    public ActionResult ManageUsers(List<int> users)
    {
        //Manage all the selected users which will appear in the List.
    
        return View()...
    }
    

    I believe you can have an array as type instead of a generic list aswell, havn't tried it myself though, the key however is to name the parameter the same as the html element id for the listbox.

    0 讨论(0)
提交回复
热议问题