2016-07-14 58 views
1

提交表單我有代碼的看法波紋管:如何在MVC

@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> 
} 

Script代碼:

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

和行動Controlller測試波紋管:

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

當我點擊提交,未找到操作DeleteItem和郵件錯誤:

因爲你沒有指定你 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

+0

是這種方法'DeleteItem'在測試控制器? –

+0

@SmitPatel:DeleteItem是TestController中的Action –

+0

'return View();'表示您在'TestController'中返回名爲'DeleteItem.cshtml'的視圖。如果你想返回一個不同的視圖,那麼你需要指定它的名稱 - '返回視圖(「MyOtherView」);' –

回答

3

這件事。

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.

+0

謝謝@Smit Patel,它的工作! –