2014-10-31 47 views
0

這是我的場景,我從數據庫中獲取數據,嘗試/捕獲它在控制器操作中,並希望將代碼和錯誤消息傳回給我的js。如何獲得MVC操作返回錯誤的狀態

//in my action 

    [HttpPost] 
public JsonResult GetWorkorderDetails(string folderno) 
{ 
    try{ 
    //do some stuff - fetch the data 

    }catch() { 
    //if error, I would like to set the JsonResult to return bad status and error message 
    } 
    } 

    //in my js 
    if(response.status == 400){ 
     //if error, output the message response.responseText and return 
    } 

任何方法來設置我的響應狀態400內的動作在catch語句?

+0

嘗試不適合。像'返回新的HttpStatusCodeResult(400,「發生異常」);' – 2014-10-31 16:28:31

+0

嗨,你可以張貼你的評論作爲答案嗎? – sarsnake 2014-10-31 16:53:24

回答

1

我會將返回類型更改爲泛型ActionResult,並在需要時返回新的HttpStatusCodeResult。

相關問題