asp.net MVC 中的 ViewData.* 和 TModel

发布于 2024-08-28 10:29:38 字数 353 浏览 4 评论 0原文

使用了一周的asp.net mvc2后,我仍然没有理解ViewData.model的优点,或者更确切地说,我如何正确地利用Viewdata。有人可以教我如何正确使用Viewdata吗?

另外,与 viewdata 关联的 TModel 是什么?如何利用TModelspark view引擎中的viewdata解释谈论了TModel,但我不能了解如何在我的项目中使用它。有人可以帮助我吗?

After a week of asp.net mvc2, I still haven’t understood the advantages of ViewData.model or rather how I can properly utilize Viewdata. Can some teach me how to use Viewdata properly?

Also what’s TModel that’s associated with viewdata? How does one utilize TModel? The viewdata explanation in spark view engine talks about TModel and I couldn’t get a clue of how I can use it in my projects. Can someone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉目亦如画i 2024-09-04 10:29:38

ViewData.Model 是您可以在控制器操作中设置并传递到视图的东西,您可以在其中像这样

<%=ViewData.Model.Description %>

<%=Model.Description %>

那样访问它,如果您传递到视图的类包含属性描述:

public ActionResult GetInstance(string id)
{
    MyContent content = GetContentFromDatastore(id);
    return View(content);
}

使用此 MyContent 类

MyContent
{
    string id;
    string description;
}

基本上您是将类的实例(设置了属性的对象,很可能从数据库中获取)发送回视图并在视图中显示其数据,视图是 ascx 或 aspx 文件,最终显示给用户/访问者。这是一个非常简单的例子,但不清楚你到底想要什么以及你已经知道多少。但在您充分了解 ASP.NET MVC 基础知识之前,请暂时不要考虑 Spark(和其他视图引擎)。

ViewData.Model is something that you can set in controller action and gets passed to the View where you can access it like this

<%=ViewData.Model.Description %>

or

<%=Model.Description %>

that is, if the class that you are passing to the View contains property Description:

public ActionResult GetInstance(string id)
{
    MyContent content = GetContentFromDatastore(id);
    return View(content);
}

with this MyContent class

MyContent
{
    string id;
    string description;
}

Basically you are sending an instance of a class (an object with its properties set, most likely taken from the database) back to the View and display its data in the View, View being the ascx or aspx file, that eventually gets display to the user/visitor. This is very simple example but it is unclear what exactly you want and how much you already know. But try to leave Spark (and other View Engines) out of the question for now until you know the ASP.NET MVC basics well.

ゝ杯具 2024-09-04 10:29:38

Mare 是正确的,您可以通过访问 ViewData.ModelName.PropertyName 项在视图中使用模型。

另外,在控制器中,您可以在 ViewData 字典中设置某些键/值对:

ViewData["Address1"] = "2222 Somewhere";

然后在视图中访问它:

<%= Html.Encode(ViewData["Address1"]) %>

显然,使用键/值对来处理所有数据并不理想,这就是为什么您可以创建您自己的类来封装数据,并将其传递到您的视图以便于操作。

Mare is correct, you can use your models in your view by accessing the ViewData.ModelName.PropertyName item.

Also while in your controller you can set certain key/value pairs in the ViewData dictionary:

ViewData["Address1"] = "2222 Somewhere";

And then access it in your view:

<%= Html.Encode(ViewData["Address1"]) %>

Obviously it wouldn't be ideal to use a key/value pair to handle all of your data, thats why you can create your own classes to encapsulate data, and pass THOSE to your view for easier manipulation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文