在 ASP.NET MVC 解决方案中使用实体框架保存下拉列表选择
我正在寻找有关下拉列表选择的合适模式以及 POCO EF 持久选择的建议。
我的视图模型中有一个 IEnumerable
列表,其中 Country 是通过 EF 加载的 POCO。视图模型上有一个 Address 属性,它采用其 Country 属性上的当前值或用户选择的值。在视图中,我通过 Html.DropdownListFor() 显示这些内容:
Html.DropDownListFor(model => model.Address.Country.Id, new SelectList(Model.Countries,"Id","Name",model.Address.Country.Id)
到目前为止一切顺利,并且一切都可以在回发时使用默认的 ModelBinder 为我提供一个填充了 Address.Country 的视图模型。但是,Address.Country 当然仅使用默认模型绑定的 Id 字段进行填充。
尝试通过 EF 将地址更新发送回数据库会失败,因为这被视为一个新对象,没有加载完整的对象图,仅加载了 Id 集。
现在,我可以通过在回发时将完整的 Country 对象从数据库加载到 Address.Country 属性中来修复此问题,然后根据所选 ID 进行保存。但这对于除了简单的对象图之外的任何事情来说似乎都是一项艰巨的工作。
我能想到的最“优雅”的解决方案是国家/地区的自定义模型绑定程序,但这需要模型绑定程序了解用于检索完整 EF 对象的存储库,这对我来说似乎不合适。我还必须对下拉列表中使用的所有其他实体重复此操作。
希望这是有道理的,并且任何有关其他人如何做到这一点的反馈都将受到赞赏。
I'm looking for advice on a decent pattern for dropdown list selection and persistence of the selection with POCO EF please.
I have a list of IEnumerable<Country>
in my view model where Country is a POCO loaded via EF. There is an Address property on the view model that takes the current or user selected value on it's Country property. Within the view I display these via a Html.DropdownListFor() thus:
Html.DropDownListFor(model => model.Address.Country.Id, new SelectList(Model.Countries,"Id","Name",model.Address.Country.Id)
So far so good and it all works on postback with the default ModelBinder providing me with a view model with the Address.Country populated. However Address.Country is of course only populated with the Id field with default model binding.
Trying to send the Address update back to the DB through EF blows up as this is seen as a new object which doesn't have it's full object graph loaded, only the Id set.
Now I can fix this by loading the full Country object from the db into the Address.Country property on postback before saving based on the selected Id. But this seems like a lot of hard work for anything beyond a simple object graph.
The most "elegant" solution I could think of would be a custom model binder for Country but then that would require the Model Binder to know about the repository for retrieving the full EF object which doesn't seem right to me. I'd also have to repeat this for all other Entities used in Dropdown lists.
Hope this makes sense and any feedback on how others are doing this would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将 Address.Country 设置为对象,EF 期望它是当前上下文一部分的完整对象,但 EF 确实识别外键:如果您的 Address 对象同时具有 CountryID 属性和 Country 属性,则它应该接受 Address只要 Address.Country 本身为 null,就会设置 .CountryID。
If you set Address.Country to an object, EF expects it to be a full object that's part of the current context, but EF does recognize foreign keys: If your Address object has both a CountryID property and a Country property, it should accept Address.CountryID being set as long as Address.Country itself is null.
在您的地址类中将国家/地区声明为虚拟
尝试这个并让我知道它是否有效,虚拟支持延迟加载,您不必显式查询
In your Address class declare country as virtual
try this and let me know if it works, Virtual supports lazyloading and you don't have to query explicitly