0

我有一个表单,其首页正确加载,但每当我尝试提交时,都会收到以下错误:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Search/DoSearch

我有一个带有以下控件的 MVC 3 表单

public class HomeController : Controller
    {


        [HttpGet]
        public ViewResult Index()
        {
            return View();
        }

        [HttpPost]
        public ViewResult Index(FormModel formModel)
        {

            return View("Thanks", formModel);
        }

    }

索引页面具有以下形式

@model RequestForm.Models.FormModel
@{
    Layout = null;
}
<html>
<head>
<link rel="Stylesheet" href="@Href("~/Content/Site.css")" type="text/css"/>
<title>Request page</title>
</head>
<body>
@using (Html.BeginForm("DoSearch", "Search", FormMethod.Post, new { @class = "form-class" })){
      @Html.LabelFor(x => x.fullName,"Full Name")
      @Html.TextBoxFor(x => x.fullName)


      @Html.LabelFor(x => x.address, "Address")
      @Html.TextBoxFor(x => x.address)

      @Html.LabelFor(x => x.phone, "Phone Number")
      @Html.TextBoxFor(x => x.phone)

      @Html.LabelFor(x => x.email,"Email")
      @Html.TextBoxFor(x => x.email)
    <br />
    <input type="submit" value="submit" />
}
</body>
</html>

我也有一个感谢查看页面

@model RequestForm.Models.FormModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Thanks</title>
</head>
<body>
    <div>
        Thank you for your submission
    </div>
</body>
</html>

为什么没有调用感谢视图页面,为什么请求的 URL 搜索/Dosearch?

4

2 回答 2

3

您的表单正在发布到控制器的DoSearch操作方法。Search您需要更改视图中的表单声明部分来解决此问题。将其更改IndexHomeController.

在你看来,改变这个

@using (Html.BeginForm("DoSearch", "Search", FormMethod.Post, new { @class = "form-class" })){

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form-class" })){

如果您不想将 class 属性赋予表单,则可以像这样简化上面的内容

@using (Html.BeginForm())
{
  // form elements
}
于 2012-05-29T20:32:53.720 回答
0

似乎您没有在控制器端未定义的提交处理程序,

如果您使用显示的目标参数提交,则需要在控制器 SearchController 中有一个操作方法 DoSearch ,否则您需要通过将表单目标参数更改为提交到主控制器中的 Index 操作方法

HTML.BeginForm("Index","Home")
于 2012-05-29T20:45:12.233 回答