【问题标题】:MVC 3 Posting form from partial viewMVC 3 从局部视图发布表单
【发布时间】:2013-03-28 18:37:45
【问题描述】:

我不知道我做错了什么,我在布局内的部分视图中有一个表单,当表单提交时我无法让控制器触发,表单只是发布到本地主机: 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");

    }
}

我确定我错过了一些非常简单的东西......

【问题讨论】:

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


    【解决方案1】:

    试试

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

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

    【讨论】:

    • 我的新东西真的很简单...... Grr
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2012-09-24
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多