如何在一个控制器中添加多个 HttpPost 方法?



我正在创建一个简单的 ASP.NET MVC Web应用程序。在我的控制器中,我有两个 HttpPost 方法,我调用两个不同的存储过程。一切都运行良好,直到我添加了第二个 HttpPost 方法。现在我对第二个 HttpPost 方法有问题 - 当我单击它("转到查看"(时,我收到一条消息

找不到匹配的视图

我的控制器:

//...
public class OrderController : Controller
{        
[ActionName("Index")]
public ActionResult Index(OrderListOfClass ttt)
{
//code
}
[HttpPost]
[ActionName("Index")]
public ActionResult Index(OrderListOfClass ttt, string send)
{
//calling stored procedure 1
}
[ActionName("Tank")]
public ActionResult Tank(OrderListOfClass ttt)
{
//code
}
[HttpPost]
[ActionName("Tank")]
public ActionResult Tank(OrderListOfClass ttt, string sendBatch)
{
//calling stored procedure 2
}
}

我的观点:

@model SlurryOrderTest.Models.OrderListOfClass
//...
@using (Html.BeginForm("Index", "Order", FormMethod.Post))
{
//textbox to be filled by user - input parameter for stored procedure 1
}
@using (Html.BeginForm("Index", "Order", FormMethod.Post))
{
//textbox which are filled by stored procedure 1
}
@using (Html.BeginForm("Tank", "Order", FormMethod.Post))
{
//textbox to be filled by user - input parameter for stored procedure 2
}
@using (Html.BeginForm("Tank", "Order", FormMethod.Post))
{
//textbox which are filled by stored procedure 2
}

为什么"坦克"操作不进入视图? 我可能犯了一些愚蠢的错误,但 C# 对我来说是一种新语言:(

听起来你需要告诉它使用哪个视图。 默认情况下,它将在 Views\Order 文件夹中查找一个名为 tank.cshtml 的文件夹。 所以我认为你的坦克方法需要这个

[HttpPost]
[ActionName("Tank")]
public ActionResult Tank(OrderListOfClass ttt, string sendBatch)
{
//calling stored procedure 2
return View("Index", ttt);
}

或者,如果您希望它转到自己的特定视图,则通过在订单文件夹中创建文件调用 Tank.cshtml 来创建坦克视图。

调用控制器方法时,需要了解的内容很少。

  1. 为了通过右键单击从控制器转到视图,您可能需要在 views 文件夹中有一个与该方法同名的视图。
  2. 在控制器中,如果有两个同名的方法,并且其中一个具有 [HttpPost] 属性,则将从视图中的 Form.Post 调用它。如果另一个方法没有属性,那么它将自动成为 get 方法。

最新更新