开发者

Dynamic(ExpandoObject) vs static type(Model) in asp.net mvc

Dynamic vs static type in asp.net mvc

Is car better than motorcycle? It all depends on the scenario you are using it. Without knowing the scenario it is not possible to make that determination!

Is Dynamic typing better than static typing for certain problems/situations.

In this case

I need to show the view when OnException() method of BaseController is call开发者_如何转开发ed. This is one time thing and i show the error page once instead of yellow screen of death.

Suppose i need to show ErrorMessage and Stacktrace on the view. As this is one time thing is it not better to use dynamic than create a error model for this.

       dynamic obj = new ExpandoObject();
       obj.ErrorMessage = "message";
       obj.StackTrace = "bla bla bal";
       return to view.

What is the best practice. Thanks for the reply.


"Is Dynamic typing better than static typing for certain problems/situations."

Of course.

"Suppose i need to show ErrorMessage and Stacktrace on the view. As this is one time thing is it not better to use dynamic than create a error model for this."

In this case you usually have a view model that can carry the errors with it. Sometimes I use a base view model class with:

public class ViewModelBase
{
      public string ErrorMessage { get; set; }

}

Most often I'll put the errors in TempData:

TempData["Errors"] = "";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜