Asp.net mvc 如何在MVC中提交表单

Asp.net mvc 如何在MVC中提交表单,asp.net-mvc,form-submit,Asp.net Mvc,Form Submit,我有以下关于视图的代码: @using (@Html.BeginForm("DeleteItem", "Test", new { id = 3 }, FormMethod.Post, new { @class = "form" })) { @Html.AntiForgeryToken() <a class="submit-process" href="javascript:void(0);"><i class="fa fa-trash-o"></i>

我有以下关于视图的代码:

@using (@Html.BeginForm("DeleteItem", "Test", new { id = 3 }, FormMethod.Post, new { @class = "form" }))
{
  @Html.AntiForgeryToken()
  <a class="submit-process" href="javascript:void(0);"><i class="fa fa-trash-o"></i> Delete</a>
}
控制器测试波纹管中的操作:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteItem(int id)
    {
        return View();
    }
单击“提交”时,未找到操作DeleteItem,消息错误:

未找到视图“DeleteItem”或其主视图,或者没有视图引擎支持搜索的位置


发生此情况是因为您尚未在中指定viewName

您的示例可能会发生这种类型的错误

The view 'DeleteItem' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Test/DeleteItem.aspx
~/Views/Test/DeleteItem.ascx
~/Views/Shared/DeleteItem.aspx
~/Views/Shared/DeleteItem.ascx
~/Views/Test/DeleteItem.cshtml
~/Views/Test/DeleteItem.vbhtml
~/Views/Shared/DeleteItem.cshtml
~/Views/Shared/DeleteItem.vbhtml
当您没有在返回视图中设置viewname时,它会自动将方法名作为viewname,并尝试在上面的位置找到它

多看看


了解更多关于

此方法是否在测试控制器中删除项?@SmitPatel:DeleteItem是TestControllerreturn视图中的操作;表示在TestController中返回名为DeleteItem.cshtml的视图。如果要返回不同的视图,则需要指定其名称-return viewmotherview;真正的问题是,为什么要在删除该项之后返回视图,因为该项已不存在,因此没有意义。您应该使用return RedirectToAction。。。。重定向到另一个视图。还可以在返回视图名称中指定视图名称。如果要重定向到与ActionName不同的视图,请参阅我的答案中的详细信息。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteItem(int id)
{
    return view('YOUR VIEW NAME');
}
The view 'DeleteItem' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Test/DeleteItem.aspx
~/Views/Test/DeleteItem.ascx
~/Views/Shared/DeleteItem.aspx
~/Views/Shared/DeleteItem.ascx
~/Views/Test/DeleteItem.cshtml
~/Views/Test/DeleteItem.vbhtml
~/Views/Shared/DeleteItem.cshtml
~/Views/Shared/DeleteItem.vbhtml