【问题标题】:Ajax.BeginForm the form cannot be specifiedAjax.BeginForm 无法指定表单
【发布时间】:2021-08-12 07:12:59
【问题描述】:

我正在尝试创建一个将使用 ASP.NET 写入数据库的视图,并且我正在使用 Ajax.BeginForm 来捕获数据。但是,每当我尝试调用表单 POST 方法时,它都会失败并出现错误 404,说明找不到资源。我在这方面找到的在线教程没有帮助,我不确定我哪里出错了。我的代码如下:

查看:AddStop.cshtml

@model FYP2._1.Models.Stop
@{
    ViewBag.Title = "Add Stop";
}

<h2>AddStop</h2>

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

<fieldset>
    @using (Ajax.BeginForm("AddStop", "MapController", new AjaxOptions
    {
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure",
        LoadingElementId = "progress"
    }))
    {
        <table id="tblPersons" cellpadding="0" cellspacing="0">
            <tr>
                <th colspan="2" align="center">Enter Details</th>
            </tr>
            <tr>
                <td>
                    @Html.Label("Forename")
                </td>
                <td>
                    @Html.TextBoxFor(b => b.Forename)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Surname")
                </td>
                <td>
                    @Html.TextBoxFor(b => b.Surname)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("PostCode")
                </td>
                <td>
                    @Html.TextBoxFor(b => b.Postcode)
                </td>
            </tr>
            <tr>
                <td>
                    @Html.Label("Route Type")
                </td>
                <td>
                    @Html.DropDownListFor(b => b.BusRoute, new List <SelectListItem>
                        { new SelectListItem { Text = "Route 1", Value = "1" },
                          new SelectListItem { Text = "Route 2", Value = "2" },
                          new SelectListItem { Text = "Route 3", Value = "3" },
                          new SelectListItem { Text = "Route 4", Value = "4" },
                          new SelectListItem { Text = "Route 5", Value = "5"}})
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>

        @Html.ActionLink("Back To Main Menu", "Index");

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10" style="color:green">
                @ViewBag.Message
            </div>
        </div>
    }
</fieldset>

控制器:MapController.cs

        // GET: Map/AddStop
        public ActionResult AddStop()
        {
            return View();
        }
        
        // POST: Map/AddStop
        [HttpPost]
        public ActionResult AddStop(Stop NewStop)
        {
            try
            {
                db.Stops.Add(NewStop);
                db.SaveChanges();
                ViewBag.Message = "Stop Added Successfully";
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return View();
        }

提前感谢您的帮助

【问题讨论】:

    标签: c# html asp.net asp.net-mvc


    【解决方案1】:

    BeginForm方法的参数中指定的控制器名称不带控制器后缀。

    应该是这样的:

    @using (Ajax.BeginForm("AddStop", "Map", new AjaxOptions
    {
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure",
        LoadingElementId = "progress"
    }))
    

    【讨论】:

      【解决方案2】:
      Add  *HttpMethod = "Post"* to your AjaxOptions to handel it on your controller.
      

      最好在你的操作中返回一个局部视图而不是视图。

       @using (Ajax.BeginForm("AddStop", "MapController", new AjaxOptions
              {
                  OnSuccess = "OnSuccess",
                  OnFailure = "OnFailure",
                  HttpMethod = "Post",
                  LoadingElementId = "progress"
              }))
      

      【讨论】:

      • 仍然出现同样的错误,您还有什么可以建议的吗?
      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      相关资源
      最近更新 更多