MultiselectList only posting first selected item RRS feed

  • Question

  • User657329123 posted

    Hi there,

    I'm trying to add a multi-select downdownlist. In my model I have

    public string Mark { get; set; }

    I realized it was wrong since only first selected value is being posted to controller. So I changed it to

    public MultiSelectList Mark { get; set; }

    Then migration is complaining that value cannot be null.

    System.ArgumentNullException: Value cannot be null.
    Parameter name: entitySet
    at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
    at System.Data.Entity.Core.Mapping.EntitySetMapping..ctor(EntitySet entitySet, EntityContainerMapping containerMapping)
    at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.AddEntitySetMapping(DbDatabaseMapping databaseMapping, EntitySet entitySet)
    at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping)
    at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping)
    at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel)
    at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
    at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
    at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
    at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
    at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
    at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
    at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
    at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
    Value cannot be null.
    Parameter name: entitySet

    Is this error have anything to do with MultiSelectList item? I wasn't getting this error before adding MultiSelectList to my model.

    Any idea what mistake am I making in implementing a multi-select dropdown list?

    In my view I have this

        <div class="form-group">
            @Html.LabelFor(model => model.Mark, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.ListBoxFor(model => model.Mark, (MultiSelectList)ViewBag.ma, new { @class = "form-control" })

    ViewBagg.ma is populated from database.


    Monday, December 17, 2018 6:29 PM

All replies

  • User1080785583 posted

    This article states that you want to model bind to a list of integers. https://visualstudiomagazine.com/articles/2017/06/09/tip-aspnet-mvc-multiselect-dropdown.aspx

    Monday, December 17, 2018 6:38 PM
  • User657329123 posted

    I don't understand this article. It is not in c#.

    Monday, December 17, 2018 6:43 PM
  • User1080785583 posted

    Try changing 

    public string Mark { get; set; } 

    public int[] Mark { get; set; }

    Here is C# example, https://www.mikesdotnetting.com/article/265/asp-net-mvc-dropdownlists-multiple-selection-and-enum-support

    Monday, December 17, 2018 6:51 PM
  • User657329123 posted

    Mark need to be string to if I do either,

    public string[] Mark { get; set; }


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

    my migration error is gone. Controller is able to get all the selected values but I do not see any column generated in my database table where I need to store this array or list of strings.

    Monday, December 17, 2018 6:58 PM
  • User1080785583 posted

    If you are referring to code first migrations you will have to include a table to hold the key, value fields. 

    If you execute 'Add-Migration sdkljfhsdf' does it throw an error?

    If you execute 'Update-Database -Verbose' does this throw an error?

    You are asking a separate question in regards to saving the values.

    Monday, December 17, 2018 7:22 PM
  • User1520731567 posted

    Hi joegreen2005,

    Mark need to be string to if I do either,

    public string[] Mark { get; set; }


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

    my migration error is gone. Controller is able to get all the selected values but I do not see any column generated in my database table where I need to store this array or list of strings.

    Now,Your problem is that you can't save the value to database.

    But you could get all the selected values in controller.

    Does your field correspond to these values?


    Do you use [NotMapped] on the field?

    I suggest you could modify your model,for example:

          public class ListBoxModel
                public IList<SelectListItem> MarkList { get; set; }//define data source of listbox 
                public List<string> Mark { get; set; }
    @Html.ListBoxFor(model => model.Mark, Model.MarkList, new { @class = "col-md-2" })

    And then:

            public ActionResult Index(ListBoxModel model)

    You could refer to these example:



    If you have any questions,please post more details,such as: model,view and controller.

    Best Regards.

    Yuki Tao

    Tuesday, December 18, 2018 9:23 AM