Home Save multiselect c#
Reply: 2

Save multiselect c#

S.QUIN Published in 2018-02-14 07:39:46Z

I use cshtml for my select:

 <div class="form-group">
    <label class="control-label col-md-2" for="Ligne">Ligne</label>
    <div class="container">
        <select name="Ligne" id="ligne" class="selectpicker" title="Select something" data-live-search="true"  multiple="multiple">
            @foreach (var item in Model.allLignes)
                <option value=@Html.DisplayFor(modelItem => item.CodeLigne)>@Html.DisplayFor(modelItem => item.CodeLigne)</option>


But when I send form, it saves only one value...

Can you help me?

my controller:

   public ActionResult Create([Bind(Include = "Id,name,begin,end,Ligne")] RT TypeRT)

 cmdPerso.CommandText = "insert into RT (name, begin, end, Ligne, CreateBy) VALUES ('" + TypeRT.name+ "','" + TypeRT.begin+ "','" + TypeRT.end+ "','" + TypeRT.Ligne+ "','" + User.Identity.Name + "')";

I want to use this select expression for edit form.

Thank you in advance.

Georg Patscheider
Georg Patscheider Reply to 2018-02-14 12:43:41Z

Work with the tools that the ASP MVC framework provides:

  • In your ViewModel, the property to which the selected values are bound must be an array. Available options can be stored in the MultiSelectList class.

    using System.Web.Mvc;
    public class MultiSelectViewModel {
        /// <summary>
        /// Selected values of the multi select.
        /// </summary>
        public string[] SelectedValues { get; set; }
        /// <summary>
        /// Possible options.
        /// </summary>
        public MultiSelectList AvailableOptions { get; set; }
  • In your Controller (GET case), assign the available options:

MultiSelectList can be instantiated by passing a collection of options and the names of the properties that hold the Value and Text of an option (e.g. "CodeLigne").

Ligne[] allLignes = DbContext.Lignes.ToArray(); // all possible options
// for edit use case: the options that have been selected during create use case
string[] previouslySelectedLigneCodes = new { "1", "3" }; 

// your ViewModel containing the multiselect properties
var vm = new MultiSelectViewModel(); 
vm.AvailableOptions = new MultiSelectList(
    allLignes, "CodeLigne", "NomLigne", previouslySelectedLigneCodes);
  • In your View, you can render the MultiSelect using ListBoxFor:

    @model MultiSelectViewModel 
    <label for="@Html.IdFor(m => m.SelectedValues)">Ligne</label>
    @Html.ListBoxFor(m => m.SelectedValues, Model.AvailableOptions, 
                          new {@class = "selectpicker", title ="Select something"})

Further reading: Step-By-Step Implementation of MultiSelectList In .NET MVC.

Racil Hilan
Racil Hilan Reply to 2018-02-14 08:30:38Z

First, change the type of your TypeRT.Ligne to List<string> (you can change string to another type if needed, like List<int>).

Now your controller will get the list of selected values, but then your query must save them. Ideally, this list should be saved in another table and you should save each value in a separate row. That's a good normalized design.

However, it seems from your code that you save the list in a single field of the same table, perhaps comma-separated? That's not a good design and it needs to be normalized like I mentioned above, but if you really want to do so then you have to join the array items using the separator that you want:

String.Join(",", TypeRT.Ligne);

If you're using .NET < 4, you have to use ToArray first:

String.Join(",", TypeRT.Ligne.ToArray());

Note: your query is vulnerable to SQL injection. Use a parameterized query instead.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.317856 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO