asp.net-mvcviewdatastrongly-typed-view

ASP.NET MVC strongly typed views or not?


What is the best practice - to use only strongly typed views without any parameters, that passes through the ViewData dictionary, or it's a not bad idea to use something like this in a view:

<%: (string)ViewData["helloMessage"]%>

Thanks.


Solution

  • You should prefer strongly typed views. In some cases you need only one string like in your example, which doesn't belong to a model, then it is OK to use it. The other way is to encapsulate this variable into a class and pass the class to the view. The result would be a strongly typed view :-)

    I personally don't like magical strings.