开发者

Does this really violate MVC Separation of Concerns

开发者 https://www.devze.com 2022-12-19 10:40 出处:网络
Simple question. I must be totally wrong but I thought worth asking this question. Is开发者_如何学JAVA accessing ViewData[“Message”] within the View correct according to separation of concerns desc

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

Is开发者_如何学JAVA 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.


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.


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.


To me, I think the concept of all three (ViewData, ViewBag and TempData) is a violation of MVC. Very convenient to use but not following the diagram found here.

The View shouldn't know what the Controller is passing. I actually asked the same question just a few minutes ago and then I found this post.

0

精彩评论

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