如何在DropDownList中使用实体对象导航属性在我强类型的ASP.NET MVC上创建和编辑视图?

时间:2023-01-27 15:13:15

I've got an Entity Data Model with Product and Family types. Each Product has one Family.

我有一个包含产品和系列类型的实体数据模型。每个产品都有一个系列。

I'm using this model with an ASP.NET MVC web site. I want Family DropDownLists on the Create and Edit Views of my Product controller.

我在ASP.NET MVC网站上使用此模型。我想在我的产品控制器的创建和编辑视图上使用Family DropDownLists。

How Do I Use Entity Object Navigation Properties in a DropDownList on my Strongly Typed ASP.NET MVC Create and Edit Views?

如何在我的强类型ASP.NET MVC创建和编辑视图的DropDownList中使用实体对象导航属性?

The following code fails...

以下代码失败...

ProductController:

ProductController的:

// POST: /Product/Create

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product p)
{
    db.AddToProduct(p);
    db.SaveChanges();
    return RedirectToAction("Index");
}

Create View:

创建视图:

<p>
    <label for="Family">Family:</label>
    <%= Html.DropDownList("Family", new SelectList((IEnumerable)ViewData["Families"], "Id", "Name"))%>
    <%= Html.ValidationMessage("Family", "*")%>
</p>

Can I do this without using a FormCollection? I would rather keep it a strongly-typed Product.

我可以在不使用FormCollection的情况下执行此操作吗?我宁愿把它作为一个强类型的产品。

3 个解决方案

#1


1  

At present state you can't use foreign keys to make your binding. You have to manualy update a relation between a product and a family. for that you should add a familyid parameter to your "post" action. And assign product.family to families.Where(f=>f.id = famylyId)

目前,您无法使用外键进行绑定。您必须手动更新产品与家庭之间的关系。为此,您应该在“post”操作中添加familyid参数。并将product.family分配给families.Where(f => f.id = famylyId)

#2


1  

Right, so you'll be needing something other than your entity object to get the referential lists back. I see you fetching a select list from the ViewData collection bag but don't see it going in. As long as you have it going in either the ViewData or a ViewModel object, you'll be fine.

是的,因此您将需要除实体对象之外的其他内容来获取参照列表。我看到你从ViewData集合包中提取了一个选择列表,但是看不到它。只要你将它放在ViewData或ViewModel对象中,你就可以了。

To the part about your foreign key... as alex stated the current rev of EF doesn't support direct exposure of the foreign keys as simple properties. I believe this is set to change in 2.0 but in the meantime do a search for faking foreign key properties (SO won't allow me to post links yet). It works for me.

关于你的外键的部分......正如亚历克斯所述,EF的当前版本不支持将外键直接暴露为简单属性。我相信这将在2.0中发生变化,但与此同时寻找伪造外键属性(SO不允许我发布链接)。这个对我有用。

#3


0  

Assuming that Product has a Families property (or is a Linq entity with a Foreing Key constraint)

假设Product具有Families属性(或者是具有Foreing Key约束的Linq实体)

<%= Html.DropDownList("Family", new SelectList(model.Families, "Id", "Name"))%>

#1


1  

At present state you can't use foreign keys to make your binding. You have to manualy update a relation between a product and a family. for that you should add a familyid parameter to your "post" action. And assign product.family to families.Where(f=>f.id = famylyId)

目前,您无法使用外键进行绑定。您必须手动更新产品与家庭之间的关系。为此,您应该在“post”操作中添加familyid参数。并将product.family分配给families.Where(f => f.id = famylyId)

#2


1  

Right, so you'll be needing something other than your entity object to get the referential lists back. I see you fetching a select list from the ViewData collection bag but don't see it going in. As long as you have it going in either the ViewData or a ViewModel object, you'll be fine.

是的,因此您将需要除实体对象之外的其他内容来获取参照列表。我看到你从ViewData集合包中提取了一个选择列表,但是看不到它。只要你将它放在ViewData或ViewModel对象中,你就可以了。

To the part about your foreign key... as alex stated the current rev of EF doesn't support direct exposure of the foreign keys as simple properties. I believe this is set to change in 2.0 but in the meantime do a search for faking foreign key properties (SO won't allow me to post links yet). It works for me.

关于你的外键的部分......正如亚历克斯所述,EF的当前版本不支持将外键直接暴露为简单属性。我相信这将在2.0中发生变化,但与此同时寻找伪造外键属性(SO不允许我发布链接)。这个对我有用。

#3


0  

Assuming that Product has a Families property (or is a Linq entity with a Foreing Key constraint)

假设Product具有Families属性(或者是具有Foreing Key约束的Linq实体)

<%= Html.DropDownList("Family", new SelectList(model.Families, "Id", "Name"))%>