2013-12-11 54 views
0

我與ASP.NET的MVC3工作,以下是我的控制器的方法,如何在無數據時顯示錯誤消息?

 public ActionResult TravelReadyAdminDownloadInvoke(int intId, int intMonth) 
    { 
     TravelReadyAdminModel objTravelReadyAdminModel = new TravelReadyAdminModel(); 
     try 
     { 
      if (intId == 3) 
      { 
       objTravelReadyAdminModel = objTravelReadyAdminModel.GetTravelReadyAdminRawExport(intId, intMonth); 
       if (objTravelReadyAdminModel.lstRawDataEntities == null) 
       { 

       } 
       else 
       { 
        objTravelReadyAdminModel.ExportTravelReadyAdmin("TRAdminRawData", objTravelReadyAdminModel.lstRawDataEntities); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      ILogManager LogManager = new LogManager(); 
      var frame = new StackFrame(0); 
      LogManager.CallLogging(frame, ex.Message, ex.StackTrace); 
      return RedirectToAction("Error", "Common"); 
     } 
     return View(); 
    } 

我需要什麼樣的代碼透露了一些內幕,如果狀態顯示錯誤消息框?

回答

0

創建_MessageBox部分像這樣(我使用的引導這裏警報):

if (TempData.ContainsKey("warning")) 
{ 
    <div class="alert alert-danger"> 
     <button type="button" class="close" data-dismiss="alert">x</button> 
     @TempData["warning"] 
    </div> 
} 

在你的佈局文件夾添加這個地方:

@Html.Partial("_MessageBox") 

在你的控制器中添加這個(我喜歡在基本控制器中稱它可以重新使用):

protected void Alert(string message) 
{ 
      // Check to see if it exists already and removes it 
    if (TempData.ContainsKey("warning")) 
     TempData.Remove("warning"); 
    TempData.Add("warning", message); 

} 

然後在你的控制器中這樣稱呼它:

catch (Exception ex) 
{ 
    ILogManager LogManager = new LogManager(); 
    var frame = new StackFrame(0); 
    LogManager.CallLogging(frame, ex.Message, ex.StackTrace); 
    Alert("Warning message"); 
}