2011-03-28 46 views
0

我正在尝试ASP.NET MVC2。我有一个名为SearchController的控制器和一个名为Search的视图文件夹,其中包含Search.aspx。 在我的控制,我有:asp.net mvc2表单发布不断扩展浏览器网址

[AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Post() 
     { 
      HPSLucene.Models.Arbitrary arb = new HPSLucene.Models.Arbitrary(); 
      arb.Title = "Post received"; 
      return View("Search",arb); 
     } 

在我看来,我有:

<form action="Search/Post" method="post"> 
    <label><% Response.Write(Model.Title); %></label> 
    <input type="Submit" Value="First" Name="submitButton"/> 
    </form> 

它工作正常,我第一次点击按钮,浏览器显示的http://localhost:1824/Search/Post的URL。但是,当我第二次点击该按钮时,浏览器URL变为http://localhost:1824/Search/Search/Post,我得到了404。我做错了什么?非常感谢。

回答

0

你需要一个/在你的行动网址的开头。

为什么不允许asp.net处理这对你和使用:

<% using (Html.BeginForm("Post", "Search")) { %> 

<label><% Response.Write(Model.Title); %></label> 
<input type="Submit" Value="First" Name="submitButton"/> 

<% } %> 

,这将给你

<form action="/Search/Post" method="post"> 
0

您可以尝试将action设置为/Search/Post,但如果您的应用安装到非根位置,则会中断。一个可靠的方法是使用BeginForm的HtmlHelper,而不是手动编写form标签使用类似

<form action="<%= Url.Content("~/Search/Post") %>" ... 
+0

感谢,这工作。然而,在这篇文章http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx然后斯科特格思里不使用这段代码和推测他的代码工作正常 - 任何想法这里发生了什么? – Journeyman 2011-03-28 12:33:48

1

您正在使用表单操作的相对URL。我建议使用UrlHelper的Html表单助手。这两个都会产生合适的绝对网址。用剃刀语法例子:

<form action="@Url.Action("post", "search")" ... 

@using(Html.BeginForm("post", "search")) 
    { 
    ... 
    }