Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How to select values inside MultiSelect (Bootstrap)? I want to sellect values inside Colors_SELECT by using a string result.

Issue: I wote the following code but it doesnt select anything. Why `Colors.Add(TempArray[i]);` is not working?

Form

<select asp-for="Colors" asp-items="@Model.Colors_SELECT " class="MultiSelect" multiple>
     ....
</select>


back-end variables

[BindProperty(SupportsGet = true)]
public List<string>? Colors { get; set; }
public SelectList? Colors_SELECT { get; set; }


    public async Task<IActionResult> OnPostAsync()
    {
        var CurrentFilters = new Dictionary<string, string>();
        var ColorsTemp = string.Join(",", Colors);
        CurrentFilters.Add("Colors", ColorsTemp);

        string query = "";
        foreach (var p in CurrentFilters)
        {
            query += $"{p.Key}={p.Value}&";
        }
        query = query.TrimEnd(query[query.Length - 1]); //remove last '&'
        var url = $"{HttpContext.Request.Path}?{query}"; // create URL
        return Redirect(url); // Send new url
    }



public async Task OnGetAsync()
{
    string result = Request.Query["Colors"];
    if (!string.IsNullOrEmpty(result))
    {
        string[] TempArray = result.Split(",");
        for (int i = 0; i < TempArray.Length; i++)
            {
                Colors.Add(TempArray[i]);
            }
    }
}


What I have tried:

following code works but idk understand why cant I add `Colors.add()` inside if statment?

 public async Task OnGetAsync()
    {
-       Colors.Add("Black");

        string result = Request.Query["Colors"];
        if (!string.IsNullOrEmpty(result))
        {
            string[] TempArray = result.Split(",");
            for (int i = 0; i < TempArray.Length; i++)
                {
                    //Colors.Add(TempArray[i]);
                }
        }
    }
Posted
Updated 9-Jan-23 14:46pm
Comments
Graeme_Grant 9-Jan-23 20:51pm    
Please refrain from asking the same when you do not get an answer in a timeframe that is not suitable for you.

Original question here: How to select values inside list (bootstrap)?[^]

Chances are that the question needs to be modified to get a response.

1 solution

Google Search is a great research tool. I did a quick Google Search: asp.net mvc multiselect dropdown - Google Search[^]

Found many examples like this one: ASP.NET MVC 5 - Bootstrap Style Multi Select Dropdown Plugin[^]. If this is not suitable, there are many more to choose from.
 
Share this answer
 
Comments
[no name] 9-Jan-23 21:21pm    
If i could have found my answer on google than there is no need for this site. yes i have search all links on google but I cant find answer to my question. Code seem right to me but for some reason Its not selecting. btw your link is to some random multiselect plugin. I am not using a plugin in my question - Thank you again
Graeme_Grant 9-Jan-23 22:09pm    
Did you download and try the code in the link provided? It is a working solution.
[no name] 10-Jan-23 9:05am    
o i bit confused, my question was Issue: I wote the following code but it doesnt select anything. Why `Colors.Add(TempArray[i]);` is not working?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900