2016-02-05 53 views
0

我想知道當在彈出時加載局部視圖時發生錯誤時是否可以重定向到錯誤頁面。我在API中顯示了部分數據。當在模態中加載局部視圖時發生錯誤時重定向到錯誤頁面

我的js代碼

@section Scripts { 
    <script> 
     $(document).ready(function() { 

      $.ajaxSetup({ cache: false }); 

      $("a[data-modal]").on("click", function (e) { 
       $('#myModalContent').load(this.href, function() { 
        $('#myModal').modal({ 
         keyboard: true 
        }, 'show'); 
       }); 
       return false; 
      }); 
     }); 

    </script> 
} 

現在它會顯示彈出裏面的錯誤頁面。它應該被重定向到另一個頁面。

EDIT

錯誤捕獲的執行

Webconfig

<httpErrors errorMode="Custom" existingResponse="Replace"> 
    <remove statusCode="403" subStatusCode="-1" /> 
    <remove statusCode="404" subStatusCode="-1" /> 
    <remove statusCode="500" subStatusCode="-1" /> 
    <error statusCode="403" path="/Error/Index/403" responseMode="ExecuteURL"/> 
    <error statusCode="404" path="/Error/Index/404" responseMode="ExecuteURL"/> 
    <error statusCode="500" path="/Error/Index/500" responseMode="ExecuteURL"/> 
</httpErrors> 

誤差控制器

public ActionResult Index(int id = 500) 
     { 
      var model = new ErrorPageViewModel(); 
      model.ErrorCode = id; 
      switch (id) 
      { 
       case 403: 
       case 404: 
        model.ErrorHeader = "Page not Found!"; 
        break; 
       default: 
        model.ErrorHeader = "Oops, something went wrong!"; 
        break; 

      } 

      return View(model); 
     } 

回答

0

.load()回調函數可以採取額外的參數瓦特這將讓你知道請求是否成功,並採取相應的行動:

$('#myModalContent').load(this.href, function (response, status, xhr) { 
    if (status == 'error') { 
     // oops some error occurred => redirect the user to your error page 
     window.location.href = '/error500'; 
    } else { 
     $('#myModal').modal({ 
      keyboard: true 
     }, 'show'); 
    } 
}); 
+0

我試過你的解決方案,但它不工作。錯誤頁面仍顯示在模式彈出。我編輯了我的問題。謝謝 –

+0

根據我已經顯示的代碼示例,模態將顯示或重定向到錯誤頁面。但不是兩個。所以我想這個問題是在你的代碼的其他部分。 –

+0

我在我的webconfig中刪除了httpErrors標籤,你的代碼工作。 –

相關問題