我有一个Action结果方法,当单击一个按钮时,该方法应该将数据添加到数据库中。虽然,当我点击按钮使用AddDetails
Action方法添加数据时,我得到以下错误:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /home/AddDetails
控制器:
namespace //not included
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Details()
{
return View();
}
[HttpPost]
public ActionResult AddDetails(Customer customer)
{
if (ModelState.IsValid)
{
var db = new CustomerContext();
db.Customers.Add(new Customer
{
ItemName = customer.ItemName,
Price = customer.Price
});
db.SaveChanges();
return RedirectToAction("Index");
}
return View(customer);
}
}
}
查看:
@{
ViewBag.Title = "Details";
}
<h2>Add Items</h2>
<div class="container">
<form class="form-signin" role="form">
<h2>Please type in Item Name and price paid</h2>
<input type="text" class="form-control" placeholder="Name" required autofocus />
<input type="number" pattern="[0-9]+([\.|,][0-9]+)?" step="0.01" min="0" class="form-control" placeholder="Price" required autofocus />
<input type="button" class="btn btn-lg btn-primary btn-block" value="Add" onclick="location.href='@Url.Action("AddDetails", "home")'" />
</form>
</div>
答案 0 :(得分:0)
您要发布到/ Customers / Details,但您的行动是/ Customers / AddDetails。您可以告诉您的表单专门发布到/ Customers / AddDetails,或者您可以将AddDetails操作重命名为Details。同时使用名为Details的GET和POST操作也没关系。发出发布请求时,将触发具有HttpPost属性的操作。当您发出get请求时,将触发没有属性或HttpGet属性的操作。
如果你想保留名称'AddDetails',你的表单应该是这样的(我使用的是Razor而不是纯HTML):
@using (Html.BeginForm("AddDetails", "Home", FormMethod.Post) {
<h2>Please type in Item Name and price paid</h2>
<input type="text" class="form-control" placeholder="Name" required autofocus />
<input type="number" pattern="[0-9]+([\.|,][0-9]+)?" step="0.01" min="0" class="form-control" placeholder="Price" required autofocus />
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Add" />
}
答案 1 :(得分:0)
我想,我输入的速度不够快......正如Neil所提到的,使用Html Helper,虽然我建议使用命名参数,这样你就不会混淆哪个是动作和控制器:
@using (Html.BeginForm(
actionName: "AddDetails",
controllerName: "Customer",
method: FormMethod.Post,
htmlAttributes: new { @class = "myclass" }))
另外,为了帮助保持路线正确(并且不需要依赖“魔术弦”),我强烈建议使用T4MVC。然后您的表单将如下所示:
@using (Html.BeginForm(MVC.Customer.AddDetails(), ...)
如果Action不存在,Razor视图甚至不会渲染。编写表单也更容易,因为Intellisense将帮助您找到所需的控制器和操作。
答案 2 :(得分:0)
在代码中进行这些更改。
namespace //not included
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult AddDetails()
{
Customer newCustomer = new Customer();
return View(newCustomer);
//right click this action and choose addview and use strongly typed
//view by choosing Customer class
}
[HttpPost]
public ActionResult AddDetails(Customer customer)
{
//whatever you want here
}
}
}
您将获得包含表单和输入textBoxes的所有语法的视图。只需要几个小的补充来满足您的UI要求。
如果有任何疑问,请随时提出更多疑问。
答案 3 :(得分:0)
您需要像Html.BeginForm
@using (Html.BeginForm("AddDetails", "Home", FormMethod.Post)
中提供操作名称
或
将您的控制器操作名称更改为Details
而不是AddDetails