Tech Off Thread

6 posts

ASP.NET MVC MultiSelect issue

Back to Forum: Tech Off
  • User profile image
    ZippyV

    I'm trying to create a listbox with multiple selected items but it doesn't work.

     

    My model:

    Model with many to many relationship

     

    Function Edit(ByVal id As Integer) As ActionResult
     Dim vm As New ViewModels.NewsItemViewModel() With {
      .NewsItem = _
    db.NewsItems.Include("NewsCategories").Single(Function(item) item.Id = id),
      .Categories = _
    New MultiSelectList(db.Categories, "Id", "Category", .NewsItem.NewsCategories)
     }
     Return View(vm)
    End Function
    

    The first param of MultiSelectList is the list of Categories, the last param is the list of selected categories.

     

    It doesn't work because nothing gets selected

    <select id="NewsItem_NewsCategories" 
    multiple="multiple" name="NewsItem.NewsCategories">
    <option value="1">Politics</option>
    <option value="2">Economy</option>
    <option value="3">Sports</option>
    </select>
    

     

    Then I read somewhere that the last param expects a list of strings with the ids. So I modified my code

    Function Edit(ByVal id As Integer) As ActionResult
     Dim vm As New ViewModels.NewsItemViewModel() With {
      .NewsItem = _
    db.NewsItems.Include("NewsCategories").Single(Function(item) item.Id = id),
      .Categories = _
    New MultiSelectList(db.Categories, "Id", "Category", .NewsItem.NewsCategories.Select(Function(c) c.id.ToString())
     }
     Return View(vm)
    End Function
    

     

    When I looked up the values during runtime I got some hope:

    Generic Forum Image

     

    But the resulting HTML is the same as what I showed in my first html sample: nothing is selected.

     

    Since the MVC code is open source you can view MultiSelectList's code: download here.

  • User profile image
    ZippyV

    The code in the View:

    <%: Html.ListBoxFor(Function(m) m.NewsItem.NewsCategories, Model.Categories)%>

  • User profile image
    Maddus Mattus

    ZippyV said:

    The code in the View:

    <%: Html.ListBoxFor(Function(m) m.NewsItem.NewsCategories, Model.Categories)%>

    I have had my share of headache's with the HTML.ListBoxFor

     

    One of the gotacha's is; don't put any ViewData[] or other stuff on your form that interferes with the name, ID's ect.

     

    Here's how I use em, that I found works:

     

    The SalesOpsManagers is just an IList<string>

     

    <%

    = Html.ListBoxFor(model => model.SalesOpsManagers, (IEnumerable<SelectListItem>)ViewData["TerritorySalesOpsManagers"], new { style = "width:250px;" })%>

     

    And the viewdata is in the form of a selectlistitem, doesnt need to be multiselectlist item

     

    hope this helps you

  • User profile image
    ZippyV

    Maddus Mattus said:
    ZippyV said:
    *snip*

    I have had my share of headache's with the HTML.ListBoxFor

     

    One of the gotacha's is; don't put any ViewData[] or other stuff on your form that interferes with the name, ID's ect.

     

    Here's how I use em, that I found works:

     

    The SalesOpsManagers is just an IList<string>

     

    <%

    = Html.ListBoxFor(model => model.SalesOpsManagers, (IEnumerable<SelectListItem>)ViewData["TerritorySalesOpsManagers"], new { style = "width:250px;" })%>

     

    And the viewdata is in the form of a selectlistitem, doesnt need to be multiselectlist item

     

    hope this helps you

    As you can see from my controller action, I don't put anything in the viewdata.

     

    I've seen people creating a list of SelectListItem using LINQ but they are all in C# and I have no idea how to translate it to VB.NET.

     

    Example:

    var friendNames = 
     (from tweet in friendTweets
     select new SelectListItem
     {
      Text = tweet.ScreenName,
      Value = tweet.ScreenName
    // Selected = True|False should also be filled in here
     })
     .ToList();
    

  • User profile image
    Sven Groot

    ZippyV said:
    Maddus Mattus said:
    *snip*

    As you can see from my controller action, I don't put anything in the viewdata.

     

    I've seen people creating a list of SelectListItem using LINQ but they are all in C# and I have no idea how to translate it to VB.NET.

     

    Example:

    var friendNames = 
     (from tweet in friendTweets
     select new SelectListItem
     {
      Text = tweet.ScreenName,
      Value = tweet.ScreenName
    // Selected = True|False should also be filled in here
     })
     .ToList();
    

    Dim friendNames = (From tweet In friendTweets _
                       Select New SelectListItem() With _
                       { _
                           .Text = tweet.ScreenName, _
                           .Value = tweet.ScreenName _
                       }).ToList()

     

    This really ain't rocket science. Tongue Out

  • User profile image
    Maddus Mattus

    ZippyV said:
    Maddus Mattus said:
    *snip*

    As you can see from my controller action, I don't put anything in the viewdata.

     

    I've seen people creating a list of SelectListItem using LINQ but they are all in C# and I have no idea how to translate it to VB.NET.

     

    Example:

    var friendNames = 
     (from tweet in friendTweets
     select new SelectListItem
     {
      Text = tweet.ScreenName,
      Value = tweet.ScreenName
    // Selected = True|False should also be filled in here
     })
     .ToList();
    

    Zippy,

     

    does the problem exist?

     

    if so, can you post your asmx or aspx?

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.