如何使用Edit形式的SelectList更新实体引用? (asp.net mvc) [英] How do I update an Entity Reference using a SelectList in edit form? (asp.net mvc)

查看:52
本文介绍了如何使用Edit形式的SelectList更新实体引用? (asp.net mvc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想弄清楚如何实现以下目标:

Just trying to figure out how do achieve the following:

我有一个编辑表单,以及用于该表单的简单视图模型。
视图模型包含一个实体,然后包含一些用于下拉列表的SelectList。

I have an edit form, and a simple viewmodel for said form. The viewmodel contains an entity and then a few SelectLists for dropdowns.

问题是这样的:
所涉及的实体具有相关的实体(称为房间)。我希望用户能够从下拉列表中选择其他房间,并相应地更改此相关实体。

The problem is this: The entity in question has a related entity (which is called room). I want the user to be able to select a different room from a dropdown and have this related entity changed accordingly.

我不能只将RoomID设置为新值,实体框架不会公开它。如果我尝试了,则UpdateModel失败。

I cant just set the RoomID to a new value and the entity framework doesn't expose it. If I try then UpdateModel fails.

因此,是否可以通过编辑表单来更新实体引用?

So, is is possible to update an entity reference via an edit form?

感谢

(开始希望我使用Linq进行SQL)

(starting to wish I'd used Linq to SQL)

(清晰度)

推荐答案

我们只是通过更新ID来做到这一点(或多或少;我们使用表示模型而不是直接绑定视图)实体,但您有主意...):

We do it by just updating the ID (more or less; we use presentation models instead of binding our views directly to entities, but you get the idea...):

SomeEntity.SomeOtherEntityReference.EntityKey =
    new EntityKey("MyEntities.SomeOtherEntities", "Id", 
        presentationModel.SomeOtherEntityId);

EF 4使它更加优雅,是的,但是您当然也可以在EF 1中做到这一点。

EF 4 makes this more elegant, yes, but you can certainly do it in EF 1, as well.

这篇关于如何使用Edit形式的SelectList更新实体引用? (asp.net mvc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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