I'm new to MVC and am designing a simple part search tool. I want the URL to update with the search terms in the controller/action/id format, like:
http://localhost/Materials/Search/PartA
However, it updates like this:
http://localhost/Materials/Search?id=PartA
When I enter the desired URL, it works. However, searching for a new part from the same window causes issues:
http://localhost/Materials/Search/PartA?id=PartB
What am I doing wrong? I thought about a redirect using javascript, but then I'd also have to check the URL string to see if the ID is already embedded in the URL. I'm sure others have dealt with this issue so just wanted to know what the best practice for this is.
Controller:
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
View:
@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>