MVC は初めてで、単純な部品検索ツールを設計しています。次のように、コントローラー/アクション/ID 形式の検索語で URL を更新します。
http://localhost/Materials/Search/PartA
ただし、次のように更新します。
http://localhost/Materials/Search?id=PartA
目的の URL を入力すると、機能します。ただし、同じウィンドウから新しいパーツを検索すると、次のような問題が発生します。
http://localhost/Materials/Search/PartA?id=PartB
私は何を間違っていますか?JavaScript を使用してリダイレクトすることも考えましたが、URL 文字列をチェックして、ID が既に URL に埋め込まれているかどうかを確認する必要があります。他の人がこの問題に対処していると確信しているので、これに対するベストプラクティスが何であるかを知りたいだけです.
コントローラ:
Namespace MyApp.Controllers
Public Class MaterialsController
Inherits System.Web.Mvc.Controller
'
' GET: /Materials
Function Index() As ActionResult
Return View()
End Function
Function Search(Optional ByVal id As String = "") As ActionResult
If String.IsNullOrWhiteSpace(id) Then
id = "Enter a part number to search."
Else
id = "Part search for " + id + "."
End If
Return View("~/Views/Materials/Search.vbhtml", Nothing, id)
End Function
End Class
End Namespace
意見:
@ModelType string
@Code
ViewData("Title") = "Search"
End Code
<h2>Search</h2>
@Code
Using (Html.BeginForm("Search", "Materials", FormMethod.Get))
@<p>@Html.TextBox("id", Nothing, New With {.maxlength = 20, .style = "width:200px"})
<input type="submit" value="Search" />
</p>
End Using
End Code
<h3>@Model</h3>