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?
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:
or this: