views:

248

answers:

1

Good Morning All,

I am developing a new ASP.net MVC web application. Part of it's functionality is to search the Parts List on the SQL Server database. I have created a ADO.net Entity Data Model as part of the solution and named it PartList. I'm using master pages and want the search control rendered on it. As such, I've created PartsForm.ascx in the Shared directory as:

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

I also created a PartsController which serves 2 purposes: 1) to render the entire parts list on the Parts page and 2) to search the parts list and return results to the PartsForm.ascx. Here's the code for PartsController:

Public Class PartsController
Inherits System.Web.Mvc.Controller

Private _entities As New Diel_inventoryEntities()

'
' GET: /Parts/

Function Index() As ActionResult
    Return View(_entities.PartList.ToList())
End Function

'
' GET: /Parts/Details/5

Function Details(ByVal id As Integer) As ActionResult
    Return View()
End Function

'
' GET: /Parts/Create

Function Create() As ActionResult
    Return View()
End Function

'
' POST: /Parts/Create

<AcceptVerbs(HttpVerbs.Post)> _
Function Create(ByVal collection As FormCollection) As ActionResult
    Try
        ' TODO: Add insert logic here
        Return RedirectToAction("Index")
    Catch
        Return View()
    End Try
End Function

'
' GET: /Parts/Edit/5

Function Edit(ByVal id As Integer) As ActionResult
    Return View()
End Function

'
' POST: /Parts/Edit/5

<AcceptVerbs(HttpVerbs.Post)> _
Function Edit(ByVal id As Integer, ByVal collection As FormCollection) As ActionResult
    Try
        ' TODO: Add update logic here

        Return RedirectToAction("Index")
    Catch
        Return View()
    End Try
End Function
Function Search(ByVal id As String, ByVal SearchType As String) As ActionResult
    If SearchType = "description" Then
        Dim SearchResult = From p In _entities.PartList _
                         Where p.PARTNAME = id _
                         Select p
        Return View(SearchResult)
    End If
    If SearchType = "NSN" Then
        Dim SearchResult = From p In _entities.PartList _
                           Where p.NSN = id _
                           Select p
        Return View(SearchResult)
    End If
    Return View("UnknownType")
End Function
End Class

Intended search control functionality: Receive input search string and conduct search on either PartName or NSN depending on which dropdownlist selection is made. I would like to output the search results to a separate page. Can anyone provide me with some help and guidance as to how to resolve this issue and create the intended functionality?

Thanks,

Sid

Clarification: I'm not understanding why I receive the error message Object Reference not Set to Instance of Object on the PartsForm.ascx file. Besides the ADO.net Entity Data Model (edmx file), do I need to create a class to define each of the fields in my model? I've seen some of that done in tutorials, but thought that the edmx file took care of that? Is that why this error message is being thrown?

Relevant Code:

<%@ 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("PARTNAME",Model.PARTNAME )%>
<%=Html.DropDownList("NSN", Model.NSN)%>
<%=Html.ValidationMessage("Part Name or NSN", "*")%>
</p>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
<% End Using%>

Snippet from PartsController:

 Function Search(ByVal id As String, ByVal SearchType As String) As ActionResult
    If SearchType = "PARTNAME" Then
        Dim SearchResult = From p In _entities.PartList _
                         Where p.PARTNAME = id _
                         Select p
        Return View(SearchResult)
    End If
    If SearchType = "NSN" Then
        Dim SearchResult = From p In _entities.PartList _
                           Where p.NSN = id _
                           Select p
        Return View(SearchResult)
    End If
    Return View("UnknownType")
End Function
Function Result(ByVal id As String, ByVal SearchResult As String) As ActionResult
    Return View("SearchResult")

End Function

After modifying PartsController and PartsForm.ascx as displayed above, the error message still persists.

+1  A: 

Still fairly vague but here is what I'm seeing.

The page is submitting to itself. not a problem but when you return from your Search controller you are not also returning the PARTNAME or NSN objects.

That may be because you left that out of your question.

If not then create another class which has your search result, PARTNAME and NSN objects and return that to the page.

The use Model.PARTNAME ect and for your products Model.Products or something like that.

If this is wrong then please provide more relevant code or a small snippet of code that fails.

EDIT

Sorry you should return SearchType in your new class back to your view not like I said NSN and PARTNAME

griegs