2010-12-18 66 views
1

假設我有這樣的跟蹤異常的實體框架與Asp.net MVC

public void Upload (Picture picture) 
    try 
     { 
      //ps is the entity framework 
      ps.AddToPictures(picture); 
      ps.SaveChanges(); 

      return picture.PictureId; 
     } 
     catch (Exception e) { 
      //some codes to bound the exception to the model 
     } 

碼我可以如何呈現的例外模型和視圖呈現出來?

回答

1

使用ModelState.AddError

例子:

catch (Exception e) { 
    ModelState.AddError("SomeErrorKey", e.Message); 
} 

然後在視圖:

<%= Html.ValidationMessage("SomeErrorKey") %> 

記不住ValidationMessage正確的過載 - 所以來看看不同的過載。

雖然我建議使用自定義異常,但您不希望在視圖中顯示「空引用異常」之類的內容。

更多關於ModelState.AddError here

+0

我會嘗試看看它是否正常工作....首先感謝。 – Seen 2010-12-18 06:46:38

+0

@Seen - 沒有問題 - 讓我們知道你如何繼續。 – RPM1984 2010-12-18 23:54:19