如何在MVC中提交表单

时间:2016-07-14 04:16:13

标签: 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> Delete</a>
}

脚本代码:

$('.submit-process').click(function () {
    if (confirm('You want delete?')) {
        $(this).closest("form").submit();
    }
    else return false;
});

控制器测试中的动作如下:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteItem(int id)
    {
        return View();
    }

当我点击提交时,找不到操作DeleteItem,并且消息错误:

  

视图&#39; DeleteItem&#39;或者找不到它的主人,或者没有视图引擎支持搜索的位置

1 个答案:

答案 0 :(得分:3)

发生此事是因为您未在

中指定viewName
[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

如果您未在返回视图()中设置viewname,则会自动将方法名称设为viewname并尝试在上面的位置找到它。

查看更多Here

详细了解Controllers and Action Methods in ASP.NET MVC Applications.