tags:

views:

114

answers:

2

I would like to create a DropDownList containing 2 items: Part Name and NSN.

This dropdownlist will be used as part of a search box control that will be a partial render as part of my master page. The user will enter their search text and select either Part Name or NSN from the dropdownlist and click submit. The query will return results based on the searchtext. I have defined PartsController and here's the pertinent portion of it:

    Function Search(ByVal searchtext As String, ByVal SearchType As String) As ActionResult
        Dim searchlist As List(Of String) = New List(Of String)
        searchlist.Add("Part Name")
        searchlist.Add("NSN")
        ViewData("searchlist") = New SelectList(searchlist)

        If SearchType = "PARTNAME" Then
            Dim SearchResult = From p In _entities.PartList _
              Where p.PARTNAME = searchtext _
              Select p
            Return View(SearchResult)
        End If
        If SearchType = "NSN" Then
            Dim SearchResult = From p In _entities.PartList _
              Where p.NSN = searchtext _
              Select p
            Return View(SearchResult)
        End If
        Return View("UnknownType")
    End Function

PartsForm.ascx is defined as follows:

    <%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of DielToolMVC.PartList)" %>
    <%=Html.ValidationSummary("Please correct the errors and try again")%>
    <%  Using (Html.BeginForm("Search", "PartsController"))%>
        <fieldset>
            <p>
                <label for="Parts">Please enter a part description or NSN.</label>
                <%=Html.TextBox("searchtext") %>
                <%=Html.DropDownList("searchlist")%>
                <%=Html.ValidationMessage("Part Name or NSN", "*")%>
            </p>
            <p>
                <input type="submit" value="Search" />
            </p>
        </fieldset>
    <% End Using%>

When I debug, I receive the following error message:

There is no ViewData item with the key 'searchlist' of type 'IEnumerable'.

I'm a bit confused as the MSDN documentation demonstrates similar examples. However, after following such examples I get this error. What am I overlooking?

A: 

Is it possible you aren't hitting the Action you think you are? Can you place a breakpoint where it adds the seachlist to the ViewData to make sure that point is hit?

Yuriy Faktorovich
+1  A: 

If this control is rendered as part of your master page then the list of static values will have to be added to the ViewData in each and every action method that will render a view with that master page. If the values are static and will not change on each call then you should just code them into you partial view like this:

<select id="searchlist" name="searchlist">
        <option value="PARTNAME" label="Part Name" />
        <option value="NSN" label="NSN" />
</select>

or this:

<%  Dim items As New List(Of SelectListItem)()
    items.Add(New SelectListItem() With {.Value = "PARTNAME", .Text = "Part Name", .Selected = True})
    items.Add(New SelectListItem() With {.Value = "NSN", .Text = "NSN"})
%>

<%=Html.DropDownList("searchlist", items)%>
Jeff French
Thanks Jeff. I do intend to render the control as part of the master page. The code is most helpful. I'm surprised at the lack of dropdownlist examples in VB that are readily available. Thanks much for your help!
SidC