MVC3 httpPost方法没有加载视图

时间:2012-05-29 20:29:28

标签: c# asp.net-mvc-3 visual-studio-2010

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

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?

2 个答案:

答案 0 :(得分:3)

您的表单已发布到DoSearch控制器的Search操作方法。您需要更改视图中的表单声明部分以解决此问题。将其更改为Index的{​​{1}}操作方法。

在你看来, 改变这个

HomeController

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

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

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

答案 1 :(得分:0)

好像你没有在控制器端定义提交处理程序,

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

来提交到家庭控制器中的Index操作方法
HTML.BeginForm("Index","Home")