ViewData的,ViewBag和TempData的违反MVC? [英] ViewData, ViewBag and TempData violates MVC?

查看:169
本文介绍了ViewData的,ViewBag和TempData的违反MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果大家都看一下MVC图,认为不应该知道的车型控制器给出出来(根据MSDN MVC概述的 https://msdn.microsoft.com/en-us/library/dd381412(v = vs.108)的.aspx ) 。对我来说,ViewData的,ViewBag和TempData的违反本和我在为什么介绍这三个概念被抓我的头。

也许我得到它错了吗?我可以用数据模型来代替这三个容易传递给视图。

想知道其他人的意见。

如果您失望投票请评论为什么。

感谢您


解决方案

  

我可以很容易地使用模型的数据传递给视图,而不是这些
  三种。


是绝对可以在这种情况下,你的看法变成一个强类型的视图,你可以看到你开始考虑有行

  @Model的entityName

如果你不想要什么(由于某种原因)你的看法予以大力在所有类型的。您如何看待在这种情况下你要传递的数据进行查看。

这是的ViewData ViewBaG 的TempData


  

认为不应该知道的车型控制器发出到


我觉得你misinter preTED这条线不能达成一致。如果您认为需要显示控件的数据(可能是一个编辑视图),然后从那里它会得到的数据,如果它不知道从哪里读取数据。

查看好歹或其他方式必须知道模型数据(无论是直接通过Model对象或使用任何这3技术的形式)。

编辑:

根据您最后的评论:


  

我不能用这个?


 公共类TestModel
{
    公共动态testproperty {搞定;组; }
}

是的,也许你可以,但是,这不是一个正确的方式。尝试创建与模型一个强类型的视图 TestModel 和使用任何脚手架模板,看看是否能脚手架生成一个正确的模板。

它不会因为内部脚手架使用反射来通过你的模特属性并相应地产生这些属性/成员控制。既然你有一个动态的属性,则其不能反映该属性。类型更改为一个静态类型如字符串,看看它会生成一个 @ Html.DisplayFor(modelItem => item.testproperty)。的这就是问题

即使你不使用任何脚手架模板,并生成你的手控;你会为您的财产是什么类型的控制?因为它TPED为动态它可以是任何东西(字符串 INT 日期时间电子邮件 ...)。

ViewBag 的ViewData 的状态管理技术,对小数据量传送的B / W的意见或控制器到的意见。你应该总是您的数据模型对象传递给你的视图通过@SteveHarris作为正确地指出。

希望这是牛逼清楚一些感觉。

If we all look at the mvc diagram, the view shouldn't know the models the controller gives out to (according to msdn mvc overview https://msdn.microsoft.com/en-us/library/dd381412(v=vs.108).aspx). To me, the ViewData, ViewBag, and TempData violates this and I have been scratching my head at why these three concepts was introduced.

Maybe I am getting it wrong? I can easily pass data to the view using the models instead of these three.

Would like to know other people's opinion.

If you down vote please comment why.

thank you

解决方案

I can easily pass data to the view using the models instead of these three.

Yes definitely you can and in such case your view becomes a strongly typed view and you can see starting of your view has a line

@Model entityName

What if you don't want (for some reason) your view to be strongly typed at all. How do you think in such case you are going to pass the data to view.

That's the purpose of ViewData or ViewBaG or TempData.

the view shouldn't know the models the controller gives out to

I think you misinterpreted this line which can't be agreed upon. If your view needs data to be displayed in controls (may be an edit view), then from where it's going to get the data if it doesn't know from where to read the data.

View someway or other way must know the model data (either in form of directly passing the Model object or using any of those 3 technique).

EDIT:

Per your last comment:

can i not use this?

public class TestModel 
{ 
    public dynamic testproperty { get; set; } 
}

Yes, probably you can but that's not a proper way. Try creating a strongly typed view with model as TestModel and use any scaffold template and see if scaffolding can generate a proper template.

It will not, since scaffolding internally uses reflection to go through your model properties and accordingly generate controls for those properties/members. Since you have a dynamic property, it will not be able to reflect that property. change the type to a static type like String and see it will generate a @Html.DisplayFor(modelItem => item.testproperty). That's the issue.

Even if you don't use any scaffolding template and generate controls by your hand; what type of control will you generate for your property? since it's tped as dynamic it could be anything (string, int, datetime, email...).

ViewBag or ViewData are state management technique to pass on small amount of data b/w the views or controller to views. You should always pass your data as Model object to your view as correctly stated by @SteveHarris.

Hope this makes t clear to some sense.

这篇关于ViewData的,ViewBag和TempData的违反MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆