从视图模型MVC2.NET传球对象,查看和访问 [英] MVC2.NET pass object from viewmodel to view and access it

查看:94
本文介绍了从视图模型MVC2.NET传球对象,查看和访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的ViewModels工作从LINQ to SQL中我有一个结构问题。

When working with Viewmodels from linq to sql I have an architectural problem.

当您从数据库对象(让我们说人),并加载它在您的视图模型。这在你的看法后,当您尝试引用的类ACCES(比方说,一个人有孩子对象,它是在数据库不同的表,和不同的DataContext对象)。你得到一个不能访问处置DataContext对象(或东西),这是合乎逻辑的,因为你加载你的对象,并设置在DataContext的,例如:

When you have an object from your db (let's say "person"), and you load it in your viewmodel. After this in your view, when you try to acces referenced classes (let's say a person has children object which is a different table in db, and a different datacontext object). You get a "cannot access disposed datacontext object (or something) which is logical, because you loaded your object and disposed the datacontext. for example:

对象 - >的foreach(儿童孩子Model.Person.Children.ToList()){}

"object" ->foreach(Child child in Model.Person.Children.ToList()){}

但你怎么能做到这样就可以了(preLOAD?)的子女或存取权限,而不必在视图中创建一个新的datacontext,并重新加载的人吗?

But how can you make it so that you can ("preload"?) the children or acces them without having to create a new datacontext, and reload the person in your View ?

推荐答案

使用 DataLoadOptions.LoadWith()

见的例子在这里:的http:// MSDN .microsoft.com / EN-US /库/ Bb386917(v = VS.90)的.aspx

您必须指定 DataLoadOptions 并执行任何查询之前分配到DataContext的实例。

You must specify DataLoadOptions and assign in to the DataContext instance before executing any queries.

这篇关于从视图模型MVC2.NET传球对象,查看和访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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