属性中加载 ADO.net 参考

发布于 2024-09-14 00:17:23 字数 1638 浏览 2 评论 0原文

我有以下问题。一个对象有一个地址,这个地址有一个国家。为了用数据库中的可用国家/地区填充下拉列表,我使用对象的部分类中的属性:

public string CountryID {
        get { return this.Addresses.Countries != null ? this.Addresses.Countries.ID.ToString() : null; }
        set { this.Addresses.Countries = Repository.Instance.GetCountryByID(Convert.ToInt32(value)); }
    }

列表本身是在 formviewmodel 中生成的:

Countries = new SelectList(repository.GetAllCountries(), "ID", "country_name", objectX.CountryID);

在视图中,它的使用方式如下:

<%= Html.DropDownList("CountryID", Model.Countries, "--select--")%><%= Html.ValidationMessage("CountryID", "*")%>

问题

当我创建一个新对象时,一切都会这样顺利进行。在向 FormViewModel 提供新对象之前,我创建该对象、一个新的(空白)地址对象和一个新的(空白)国家对象。我将其传递给 FormViewModel 中的视图,然后可以根据需要创建对象。 当我想使用相同的部分形式和相同的属性 CountryID 编辑此对象时,问题就会出现。它抱怨没有地址,所以我必须添加

this.AddressReference.Load()

,以便属性变为:

public string CountryID {
        get { this.AddressesReference.Load(); return this.Addresses.Countries != null ? this.Addresses.Countries.ID.ToString() : null; }
        set { this.AddressesReference.Load(); this.Addresses.Countries = Repository.Instance.GetCountryByID(Convert.ToInt32(value)); }
    }

但现在我的 Create 方法不再起作用,因为在创建时对象尚未绑定到对象上下文(它仍然必须保存到数据库),这

this.addressReference.Load() 

会造成麻烦。

问题

有没有一种方法可以使用上面使用的一个表单(部分视图)和一个属性来创建和编辑对象?


PS 我开始不喜欢 MVC1 中的 ADO.net 延迟加载,我想这在 MVC2 中已修复,对吧?

I have the following problem. An object has an address, and this address has a country. To fill a dropdownlist with the available countries in the DB I use a property in the partial class of the object:

public string CountryID {
        get { return this.Addresses.Countries != null ? this.Addresses.Countries.ID.ToString() : null; }
        set { this.Addresses.Countries = Repository.Instance.GetCountryByID(Convert.ToInt32(value)); }
    }

The list itself is generated in the formviewmodel by:

Countries = new SelectList(repository.GetAllCountries(), "ID", "country_name", objectX.CountryID);

and in the view it is used like this:

<%= Html.DropDownList("CountryID", Model.Countries, "--select--")%><%= Html.ValidationMessage("CountryID", "*")%>

The problem

When i create a new object all goes well this way. Before I give a new object to my FormViewModel I create the object, a new (blank) address object and a new (blank) Countries object. This I pass to the view in the FormViewModel and I can create the object as is needed. The problem comes up when I want to edit this object with the same partial form and the same Property, CountryID. It complains there is no address so I have to add

this.AddressReference.Load()

so the property becomes:

public string CountryID {
        get { this.AddressesReference.Load(); return this.Addresses.Countries != null ? this.Addresses.Countries.ID.ToString() : null; }
        set { this.AddressesReference.Load(); this.Addresses.Countries = Repository.Instance.GetCountryByID(Convert.ToInt32(value)); }
    }

But now my Create method does no longer work as at time of creation the object is not yet bound to an object context (it still has to be saved to the DB), and the

this.addressReference.Load() 

is causing troubles.

Question

Is there a way I can use one Form (partial view), and one Property as used above for creation as well as editing the object?


P.S. I'm starting to dislike ADO.net lazy loading in MVC1, I guess this is fixed in MVC2 right?

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

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

发布评论

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

评论(1

最美不过初阳 2024-09-21 00:17:23
public string CountryID {
        get {
            if (this.AddressesReference.EntityKey != null) {
                this.AddressesReference.Load();
            } return this.Addresses.Countries != null ? this.Addresses.Countries.ID.ToString() : null;
        }
        set {
            if (this.AddressesReference.EntityKey != null) {
                this.AddressesReference.Load();
            } this.Addresses.Countries = Repository.Instance.GetCountryByID(Convert.ToInt32(value));
        }
    }

似乎在做我想做的事,尽管我认为这有点丑陋。更好的选择可能总是发布在这里:)。

public string CountryID {
        get {
            if (this.AddressesReference.EntityKey != null) {
                this.AddressesReference.Load();
            } return this.Addresses.Countries != null ? this.Addresses.Countries.ID.ToString() : null;
        }
        set {
            if (this.AddressesReference.EntityKey != null) {
                this.AddressesReference.Load();
            } this.Addresses.Countries = Repository.Instance.GetCountryByID(Convert.ToInt32(value));
        }
    }

Seems to do what I want, though I consider it kind of ugly. Better options may always be posted here :).

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