2

using multiSelectList
select list displaying, but selected items are not selected.
Debuged code, at the call for ListBox() the multiSelectList member,selectedMembers is populated correctly.Help would be great!
Update
Changing name of list box from "members" to anything else makes this work...i.e @Html.ListBox("AnythingExceptmembers", Model.members)
Model

    public class GroupProfileViewModel
{
    //primary key
     public int Id { get; set; }

    [StringLength(55, MinimumLength = 3)]
    [Display(Name = "Group Name")]
    public string name { get; set; }

    [StringLength(55, MinimumLength = 3)]
    [Display(Name = "Group Description")]
    [DataType(DataType.MultilineText)]
    public string description { get; set; }

    [Display(Name = "Date Group Created")]
    public DateTime dateTimeCreated { get; set; }

    public virtual UserAccountModel creator { get; set; }

    public MultiSelectList members { get; set; }

    public IEnumerable<GroupModel> groups { get; set; }
}

Controller

public ActionResult GetGroupProfile(string groupId)
    {     
            GroupProfileViewModel model;

            ...

            model.members = new  MultiSelectList(db.UserAccounts.ToList(), "Id", "fullName",  groupProfile.members.Select(p => p.Id).ToArray());
            return PartialView("_groupProfilePartial", model);
    }

Partial View

@model MiTasks.Models.GroupProfileViewModel
<form id="frm_groupProfileForm" method="post" action="/Groups/UpdateGroupProfile">
   ...
   <div class="row">
      <div class="col-md-12" style="margin-top:3em;">
         @Html.ListBox("members", Model.members) 
      </div>
   </div>
</form>

Partial View Result

<div class="row">
   <div class="col-md-12" style="margin-top:3em;">
      <select id="members" multiple="multiple" name="members">   
         <option value="1">user1Fname</option>
         <option value="2">user2Fname</option>
      </select> 
   </div>
</div>

Debug Picture showing members enter image description here

michaelBurns
  • 77
  • 1
  • 10
  • You need to show your model (unless `members` an array of value types, this wont work) –  Jun 21 '15 at 22:45
  • Model.members is a MultiSelectList. and I just found the problem (after 2 hours), for what ever reason, you can't name the listbox "members" (same name as the multiselectlist...changing it makes it work, error is reproducible.. – michaelBurns Jun 21 '15 at 22:51
  • There is an 'edit' link at the bottom of the question :). Also refer this similar [question/answer](http://stackoverflow.com/questions/30964919/modelstate-isvalid-false/30965017#30965017) –  Jun 21 '15 at 22:53
  • thanks found it and solved problem, thanks for help tho :) – michaelBurns Jun 21 '15 at 22:55
  • I see you found it. You solution may show the correct selection when you render the page, but it will fail as soon as you post (unless you access `AnythingExceptmembers` from the `Request.Form` values). See the link in my previous comment for the correct way to solve this. –  Jun 21 '15 at 22:56
  • using FormCollection and looping over the members, working, but youre solution is likely the more elegant way :) – michaelBurns Jun 21 '15 at 23:06
  • What a dreadful anti-MVC approach. Best of luck. –  Jun 21 '15 at 23:08
  • yep your right, was not sure how to bind return values of multiSelectList, but using your example and changing "SelectedMusicStyle" to a list works. Thanks :) no more FormCollection – michaelBurns Jun 21 '15 at 23:15

0 Answers0