2010-12-18 39 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