Asp.net mvc 使用ASP.net MVC和ADO.net实体数据模型搜索零件表时,错误对象引用未设置为对象的实例

Asp.net mvc 使用ASP.net MVC和ADO.net实体数据模型搜索零件表时,错误对象引用未设置为对象的实例,asp.net-mvc,ado.net-entity-data-model,Asp.net Mvc,Ado.net Entity Data Model,大家早上好 我正在开发一个新的ASP.net MVC web应用程序。它的部分功能是在SQL Server数据库中搜索明细栏。我创建了一个ADO.net实体数据模型作为解决方案的一部分,并将其命名为PartList。我正在使用母版页,并希望在其上呈现搜索控件。因此,我在共享目录中创建了PartsForm.ascx,如下所示: <%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of DielTool

大家早上好

我正在开发一个新的ASP.net MVC web应用程序。它的部分功能是在SQL Server数据库中搜索明细栏。我创建了一个ADO.net实体数据模型作为解决方案的一部分,并将其命名为PartList。我正在使用母版页,并希望在其上呈现搜索控件。因此,我在共享目录中创建了PartsForm.ascx,如下所示:

<%@ 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%>

如上所示修改PartsController和PartsForm.ascx后,错误消息仍然存在

仍然相当模糊,但这是我看到的

页面正在提交给自己。这不是问题,但当您从搜索控制器返回时,您也不会返回PARTNAME或NSN对象

那可能是因为你把这个问题忘了

如果没有,则创建另一个包含搜索结果、PARTNAME和NSN对象的类,并将其返回到页面

将Model.PARTNAME ect和用于产品Model.products或类似的内容

如果这是错误的,那么请提供更多相关的代码或失败的代码片段

编辑


很抱歉,您应该将新类中的SearchType返回到您的视图,而不是像我说的NSN和PARTNAME

请添加您遇到问题的位置。你的问题含糊不清。
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
<%@ 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%>
 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