如何从表单值在MVC中的URL中嵌入ID?

时间:2013-08-19 12:09:13

标签: vb.net asp.net-mvc-4 razor

我是MVC的新手,正在设计一个简单的零件搜索工具。我希望使用控制器/操作/ ID格式的搜索条件更新URL,例如:

http://localhost/Materials/Search/PartA

然而,它更新如下:

http://localhost/Materials/Search?id=PartA

当我输入所需的网址时,它会有效。但是,从同一窗口中搜索新零件会导致问题:

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>

3 个答案:

答案 0 :(得分:1)

如果您希望文本框中的ID位于URL中,则需要在URL中使用id发出请求,而不是在表单中发布值。您将需要使用一些javascript来构造URL并从文本框中获取值。我更喜欢使用jQuery。您可以删除表单并将提交按钮更改为常规按钮,并执行以下操作:

HTML

@Html.TextBox("id", Nothing, New With {.maxlength = 20, .style = "width:200px"})
<input type="button" id="search" value="Search" />

的Javascript

// this is jQuery's short hand and analogue to document.ready
$(function () {

    // attach a click handler to the Search button
    $('#search').click(function () {

        // make a request to /Materials/Search with the id of the text box
        $(location).attr('href', '/Materials/Search/' + $('#id').val());

    }):

});

答案 1 :(得分:0)

检查路由映射类(RouteConfig)以整理网址结构。

如果您不知道查询字符串是什么,那么在表单上使用GET操作会有很多缺点:http://www.w3schools.com/tags/att_form_method.asp

尝试将表单操作更改为POST,然后从搜索方法中,您可以执行RedirectToAction到Result ActionMethod(这次接受GET),将您从帖子中获取的值作为GET参数传递。 这样你可以在POST中使用ViewModel,FORM将是安全的,通常你可以更好地控制网址。

答案 2 :(得分:0)

在实现渐近故障的建议后,这是我修改后的视图。我更改了添加javascript事件的方法,以便与早于IE9的版本兼容。我还删除了表单标记,并在按下回车键时添加了一个javascript方法来调用重定向函数。

    @ModelType string

    @Code
       ViewData("Title") = "Search"
    End Code

    <h2>Search</h2>


    <p>
        <input id="searchString" type="text" onkeypress="checkForEnter()" style ="width:200px" maxlength="20">
        <input type="button" id="search" value="Search" onclick="searchPart()" />
    </p>

    <h3>@Model</h3>


<script type="text/javascript">
   function searchPart() {

        // make a request to /Materials/Search with the id of the text box
       $(location).attr('href', '/Materials/Search/' + $('#searchString').val());
   };

   function checkForEnter() {
       $("#searchString").keyup(function (event) {
           if (event.keyCode == 13) {
               $("#search").click();
           }
       });
   };

</script>