We help IT Professionals succeed at work.

show all selected options in Multiselect mvc c# after the submit

lulu50
lulu50 asked
on
Hi,

How can I show all selected items after the submit?

Can someone help me and tell me what I'm doing wrong please?

Model :


        public IEnumerable<SelectListItem> VersionList { get; set; }

        public List<string> SelectedVersion { get; set; }


      public IEnumerable<SelectListItem> GetVersion()
        {
            var GetVersion = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().GroupBy(x => x.Version).Select(y => y.First()).Distinct();

            return GetVersion
                 .Select(x => new SelectListItem
                 {
                     Text = x.Version,
                     Value = x.Version.ToString(),
                 })
                .Distinct()
                .OrderBy(x => x.Text);
        }

Open in new window




controller: 


 [HttpPost]
  public ActionResult SearchRules(CABRSearchRules model, string[] SelectedVersion)
        {

              model.VersionList = _careAdvanceBusinessRulessService.GetVersion();
            model.SelectedVersion = SelectedVersion.ToList();

      return View(model);
        }

Open in new window




        $('#SelectedVersion').multiselect({
            onInitialized: function (option, checked) {
                var values = [];
                var dropdown = $('#SelectedVersion').siblings('.multiselect-container');
                $('#SelectedVersion option').each(function () {
                    values.push($(this).text());
                });
            },
            enableFiltering: true,
            buttonWidth: '350px',
            maxHeight: 200,
            enableCaseInsensitiveFiltering: true,
        }); 



 @Html.DropDownListFor(m => m.SelectedVersion, new SelectList(Model.VersionList, "Value", "Text"), new { @multiple = "multiple" } )

Open in new window

Comment
Watch Question

Systems architect
Top Expert 2006
Commented:

Here your Model:

    public class SearchModel 
    {   
        // This property will hold SelectedVersion, selected by user 
        [Required] 
        [Display(Name = "SelectedVersion")] 
        public List<string> SelectedVersion { get; set; } 
 
        // This property will hold all available VersionList for selection 
        public IEnumerable<SelectListItem> VersionList { get; set; } 
    }


Here your Controller:

    public class SearchController : Controller 
    { 
        // 
        // 1. Action method for displaying 'Search' page 
        // 
        public ActionResult Search() 
        { 
            // Let's get all versions that we need for a DropDownList 
            var versions = GetAllVersions(); 
 
            var model = new SearchModel(); 
 
            // Create a list of SelectListItems so these can be rendered on the page 
            model.versions = GetSelectListItems(versions); 
 
            return View(model); 
        }	 
	 
        // 
        // 2. Action method for handling user-entered data when 'Search' button is pressed. 
        //	 
	[HttpPost] 
        public ActionResult Search(Model model) 
        { 
            // Get all versions again 
            var versions = GetAllVersions(); 
 
            // Set these versions on the model. We need to do this because 
            // only the selected value from the DropDownList is posted back, not the whole 
            // list of versions 
            model.VersionList = GetSelectListItems(versions); 
 
 
            // Rrender the page again, 
            // keeping the data user has entered by supplying the model. 
            return View("Search", model); 
        } 
		 
		 
		public IEnumerable<SelectListItem> GetAllVersions() 
        { 
            var GetVersion = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().GroupBy(x => x.Version).Select(y => y.First()).Distinct(); 
 
            return GetVersion 
                 .Select(x => new SelectListItem 
                 { 
                     Text = x.Version, 
                     Value = x.Version.ToString(), 
                 }) 
                .Distinct() 
                .OrderBy(x => x.Text); 
        } 
		 
		 
		private IEnumerable<SelectListItem> GetSelectListItems(string[] elements) 
        { 
            // Create an empty list to hold result of the operation 
            var selectList = new List<SelectListItem>(); 
 
            // For each string in the 'elements' variable, create a new SelectListItem object 
            // that has both its Value and Text properties set to a particular value. 
            // This will result in MVC rendering each item as: 
            //     <option value="State Name">State Name</option> 
            foreach (var element in elements) 
            { 
                selectList.Add(new SelectListItem 
                { 
                    Value = element, 
                    Text = element 
                }); 
            } 
            return selectList; 
        } 
   }


And here your View dropdown list:

@Html.DropDownListFor(m => m.SelectedVersion, Model.VersionList, new {  @class = "form-control", @multiple = "multiple" } )


No need for jQuery for that dropdown.





lulu50Web application

Author

Commented:
Zvonko,

Oh my God

I finally got it to work!!!

Thank you for all your help!!

Thank you and Thank you
lulu50Web application

Author

Commented:
Thank you for all your time!!

Thank you so much
ZvonkoSystems architect
Top Expert 2006

Commented:

I am glad that I could help you  :  )