找不到MVC 5 Ajax BeginForm POST 404

时间:2016-09-19 20:08:48

标签: jquery ajax asp.net-mvc asp.net-mvc-5

我的MVC应用程序出了问题。

我的控制器:

'Published'

我的观点:

 public class CustomerController : Controller
{

    public ActionResult Index()
    {
        return View();
    }


    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Customers customers)
    {
        //do something
        return View();
    }
}

我在其他视图中使用此表单:

    @model Insurance_System.Models.Customers
<script src="~/Scripts/jquery-3.1.0.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> 

<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Dodawanie nowego klienta</h4>
            </div>
            <div class="modal-body">
                @using (Ajax.BeginForm("Create", "CustomerController", null, new AjaxOptions { HttpMethod = "POST" }))
                {
                    @Html.AntiForgeryToken()
                    <div class="form-horizontal">
                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">
                            <div class="col-md-4">
                                @Html.TextBoxFor(model => model.FirstName, new { @required = "require", @class = "form-control", placeholder = @Resources.InsuranceCustomer.FirstName })
                                @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                            </div>
                            <div class="col-md-4">
                                @Html.TextBoxFor(model => model.LastName, new { @required = "require", @class = "form-control", placeholder = @Resources.InsuranceCustomer.LastName })
                                @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                            </div>
                        </div>

                  // and other field form
                    </div>

                    <div class="modal-footer">
                        <button type="submit" class="btn btn-default" data-dismiss="modal">Close</button>
                        <input type="submit" value="@Resources.Common.Save" class="btn btn-success" />
                    </div>
                }
                @*<div>
                        @Html.ActionLink("Back to List", "Index")
                    </div>*@
            </div>
        </div>
    </div>
</div>

在bootstrap modalpopup上创建CreateModel(form)。 当我填写表单并提交更改时,浏览器会显示错误:

  

POST http://localhost:8001/CustomerController/Create 404(未找到)    - [jquery-3.1.0.js:9392]

我开始玩MVC,到目前为止我遇到了问题:)

2 个答案:

答案 0 :(得分:4)

指定控制器名称时,不需要Controller后缀。

使用Customer代替CustomerController

@using (Ajax.BeginForm("Create", "Customer", null,new AjaxOptions { HttpMethod = "POST" }))
{

}

答案 1 :(得分:0)

有时 Ajax.BeginForm 不起作用,并返回 404错误,因为文件太大,无法上传。

  1. 尝试上传较小的文件;
  2. 如果第一步可行,请尝试更改web.config上传大小;

遇到类似问题时,我找到了这种解决方案