大家早上好,
我正在开发一个新的 ASP.net MVC Web 应用程序。它的部分功能是在 SQL Server 数据库中搜索部件列表。我创建了一个 ADO.net 实体数据模型作为解决方案的一部分,并将其命名为 PartList。我正在使用母版页并希望在其上呈现搜索控件。因此,我在 Shared 目录中创建了 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,它有两个目的:1)在 Parts 页面上呈现整个部件列表,2)搜索部件列表并将结果返回到 PartsForm.ascx。这是 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
预期的搜索控制功能:接收输入搜索字符串并根据选择的下拉列表对 PartName 或 NSN 进行搜索。我想将搜索结果输出到单独的页面。谁能就如何解决此问题并创建预期功能向我提供一些帮助和指导?
谢谢,
席德
澄清:我不明白为什么我收到错误消息 Object Reference not Set to Instance of Object on the PartsForm.ascx 文件。除了 ADO.net 实体数据模型(edmx 文件)之外,我是否需要创建一个类来定义模型中的每个字段?我已经在教程中看到了其中的一些内容,但认为 edmx 文件可以解决这个问题?这就是抛出此错误消息的原因吗?
相关代码:
<%@ 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%>
来自 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
如上图修改 PartsController 和 PartsForm.ascx 后,错误信息仍然存在。