2014-02-07 24 views

回答

0

在HomeController.cs附加模型狀態錯誤文件中像

try 
    { 
     methodThrowsError(); 
    } 
    catch(SomeException ex) 
    { 
    ModelState.AddModelError("", ex); 
    } 
return View(yourmodel); 

和CSHTML頁面顯示錯誤捕獲部分像

@Html.ValidationSummary() 

這裏的異常處理的example

9

如果你想顯示在同一個視圖中的錯誤消息:

[HttpPost] 
public ActionResult Add(Model model) 
{ 
    try 
    { 
     // some code... 
     return RedirectToAction("Success"); 
    } 
    catch (SomeException ex) 
    { 
     ModelState.AddModelError("", ex.Message); 
     return View(model); 
    } 
} 

和裏面的Add.cshtml視圖中,您可以使用在ValidationSummary助手來顯示錯誤消息:

@Html.ValidationSummary() 
0

只需創建會話變量在您的控制器中 AS:

public ActionResult DeleteConfirmed(int id) 
    { 
    try 
     { 
      //////whatever code u want 
      return RedirectToAction("Index"); 
     } 
     catch (Exception ex) 
     { 
      string m="mysession"; 
      Session["dep"] = m;} 
return view();} 

,並把它作爲一個標誌變量.. 然後在Razor視圖 顯示您的留言:

if (Session["dep"] != null) 
{ 
    <h1>Whatever message you want to show in your view</h1> 

} 

率我的回答,請,如果它是有益的

相關問題