MVC3 httpPost 方法未加载视图

本文关键字:加载 视图 方法 httpPost MVC3 | 更新日期: 2023-09-27 18:35:44

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

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?

MVC3 httpPost 方法未加载视图

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

在你看来,更改此内容

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

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

如果你不想给你的表单赋予类属性,你可以像这样简化上面的内容

@using (Html.BeginForm())
{
  // form elements
}

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

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

HTML.BeginForm("Index","Home")