【发布时间】:2016-07-14 04:16:12
【问题描述】:
我在下面的视图中有代码:
@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,并且消息错误:
未找到视图“DeleteItem”或其主视图,或者没有视图引擎支持搜索到的位置
【问题讨论】:
-
这个方法
DeleteItem在测试控制器中吗? -
@SmitPatel : DeleteItem 是 TestController 中的操作
-
return View();表示您在TestController中返回名为DeleteItem.cshtml的视图。如果要返回不同的视图,则需要指定其名称 -return View("MyOtherView"); -
真正的问题是你为什么要在删除项目后返回视图(这没有意义,因为项目不再存在。你应该使用
return RedirectToAction(....)重定向到另一个视图。 -
如果您想重定向到名称与您的
ActionName不同的视图,请在return view("ViewName")中指定您的Viewname,在我的回答中查看更多信息。