MVC5 drop-down box (Multiple choices) and mvc5 drop-down box (Multiple choices)
1. Model
[Display (Name = "")] [Required] public int [] job {get; set;} // job attribute public IEnumerable <Item> joblist {get; set ;} // multi-choice box attributes
2. cotroller
[Description ("job binding")] [LoginAllowView] private List <Item> bindPosts () {StringBuilder sb = new StringBuilder (); sb. append ("SELECT r01, r02,"); sb. append ("FROM rac_file"); // query the position id and name DataTable dt = sqlHelper. getData (sb. toString (); var factorOptions = dt. asEnumerable (). select (row => new Item {Name = Utils. objToStr (row ["r01"]), Id = Utils. objToInt (row ["r02"], 0 )}). toList (); return factorOptions ;}
Public ActionResult aaai000000info () {var m = new jobModel (); m. joblist = bindPosts (); // bind the position retutn view (m );}
3. View
<div class="form-group"> @Html.LabelFor(m => m.Positions, new { @class = "col-sm-2 control-label" }) <div class="col-sm-10"> @Html.ListBoxFor(model => model.job,new MultiSelectList(Model.joblist,"Id", "Name"), new { @class = "form-control select2", multiple = "multiple", style = "width: 100%;" }) <span id="states-loading-progress" style="display: none;">Please wait..</span> @Html.ValidationMessageFor(m => m.job, "", new { @class = "text-danger" }) </div> </div>