views:

68

answers:

2

Simple question. I must be totally wrong but I thought worth asking this question.

Is accessing ViewData[“Message”] within the View correct according to separation of concerns described in MVC?

For example, in Controller:

ViewData[“Message”] =  “Display this message”;

Within View we call

<%= ViewData[“Message”]  %>

The alternative (does not violates Separation of Concerns) is to have the Message set in the view model.

For example in Controller:

UserViewModel.Message = “Display this message”

Within View we call

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<UserViewModel>" %>
<%= Html.TextBox("Message", Model Message)%>

Any ideas greatly appreciated.

+1  A: 

I don't see how these should be different, other than the last one being strongly typed? You pass data along to the view, and let the view do its thing.

Luhmann
+3  A: 

The difference between ViewData and Model is that the former is untyped property bag and the latter is a strongly typed object. But they both act as 'models' for your View.

Franci Penov