みんなおはよう、
新しい ASP.net MVC Web アプリケーションを開発しています。その機能の一部は、SQL Server データベースでパーツ リストを検索することです。ソリューションの一部として ADO.net Entity Data Model を作成し、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%>
また、2 つの目的を果たす 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 のいずれかで検索を実行します。検索結果を別ページに出力したい。この問題を解決し、意図した機能を作成する方法について、誰か助けとガイダンスを提供してもらえますか?
ありがとう、
シド
明確化: PartsForm.ascx ファイルで Object Reference not Set to Instance of Object というエラー メッセージが表示される理由がわかりません。ADO.net Entity Data Model (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 を変更した後も、エラー メッセージは引き続き表示されます。