MVC 3从局部视图发布表格

时间:2013-03-28 10:20:00

标签: asp.net-mvc-3 forms user-controls

我无法弄清楚我做错了什么,我在布局中的局部视图中有一个表单,并且在提交表单时我无法触发控制器,表单只是发布到localhost: 54719 / FormController / ExecutePost,错误

The controller for path '/FormController/ExecutePost' was not found or does not implement IController.

布局代码:〜/ Views / Shared / _Layout.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>
        @ViewBag.PageTitle
    </title>
    <link rel="stylesheet" media="all" href="/Content/style.css">
</head>
<body>
    @Html.Partial("_Header");
    <!-- / header -->
    @Html.Action("Header", "Menu")
    <!-- / navigation -->
    @Html.Partial("_Form");
 <!--Form -->
    <div id="body">
        @RenderBody();
    </div>
    <!-- / body -->
    @Html.Partial("_Footer")
    <!-- / footer -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>    
    <script src="/Content/js/plugins.js"></script>
    <script src="/Content/js/main.js"></script>
</body>
</html>

表单代码:〜/ Views / Shared / _Form.cshtml

@model MathsDoctor.Models.FormModel

@using (Html.BeginForm("ExecutePost", "FormController", FormMethod.Post))
{

<div class="form" id="find-form">
    <fieldset><label>Telephone:</label>
        @Html.TextBoxFor(model => model.Telephone)
        @Html.ValidationMessageFor(model => model.Telephone)
    </fieldset>
    <fieldset><label>Name:</label>
        @Html.TextBoxFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </fieldset>
    <fieldset><label>Postcode:</label>
        @Html.TextBoxFor(model => model.Postcode)
        @Html.ValidationMessageFor(model => model.Postcode)
    </fieldset>
    <fieldset><input type="submit" value="SUBMIT">
    </fieldset>
</div>
}

表单控制器:〜/ Controllers / FormController.cs

public class FormController : Controller
{
    //
    // GET: /FindTutorForm/

    [HttpPost]
    public ActionResult ExecutePost(Models.FormModel formModel)
    {
        if (ModelState.IsValid)
        {
            return Redirect("/thanks/");
        }

        return PartialView("~/Views/Shared/_Form.cshtml");

    }
}

我确信我错过了一些非常简单的事情......

1 个答案:

答案 0 :(得分:2)

尝试

Html.BeginForm("ExecutePost", "Form", FormMethod.Post)

您必须删除“控制器”部分,因为它已自动映射。