【发布时间】: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