Modelbinder 创建了不必要的外部关系..如何解决?
我有一个与制造商具有外键关系的产品实体。我想将添加新产品的用户是否想要设置制造商设为可选。
在我的创建视图中,
<%= Html.LabelFor(p => p.Manufacturer.Name) %>
<%= Html.EditorFor(p => p.Manufacturer.Name) %>
在创建操作中,
public ActionResult Create(Product product) {
if (product.Manufacturer != null &&
!String.IsNullOrEmpty(produnt.Manufacturer.Name) &&
!String.IsNullOrWhiteSpace(produnt.Manufacturer.Name)) {
Manufacturer manufacturer = _session.Single<Manufacturer>(m => m.Name.Equals(produnt.Manufacturer.Name));
if (manufacturer == null) {
_session.Add(product.Manufacturer);
}
product.Manufacturer = manufacturer;
}
_session.Add(product);
}
在制造商表中,我只有 ID 和名称列。该名称设置为 NOT NULL。每个产品都有ManufacturerID,可以为NULL。我已经从Manufacturer.ID <=> 添加了FK 关系产品.制造商ID。
我还使用带有模型元数据的 DefaultValue 数据注释将 Product.ManufacturerID 的默认值设置为 null。
当我在创建产品时指定一些制造商时,这很好用。但是,当我将其留空时,会为数据库中的 Product.ManufacturerID 分配一个 ID,但不会使用该 ID 创建制造商。调试器显示 Product.Manufacturer.Name 为 null,但由于某种原因 Product.Manufacturer.ID 和 Product.ManufacturerID 设置为 0(不为 null)。当未指定制造商时,如何才能使其保留 Product.ManufacturerID null ?
分离制造商实体并使操作看起来像这样会是一个更好的主意吗?
public ActionResult Create(Product product, Manufacturer manufacturer) {
// ...
}
我想过度依赖模型绑定器不是一个好主意。
I have a Product entity that has a foreign key relationship to Manufacturer. I want to make it optional whether user adding a new product wants to set a manufacturer.
In my create view I have,
<%= Html.LabelFor(p => p.Manufacturer.Name) %>
<%= Html.EditorFor(p => p.Manufacturer.Name) %>
In create action,
public ActionResult Create(Product product) {
if (product.Manufacturer != null &&
!String.IsNullOrEmpty(produnt.Manufacturer.Name) &&
!String.IsNullOrWhiteSpace(produnt.Manufacturer.Name)) {
Manufacturer manufacturer = _session.Single<Manufacturer>(m => m.Name.Equals(produnt.Manufacturer.Name));
if (manufacturer == null) {
_session.Add(product.Manufacturer);
}
product.Manufacturer = manufacturer;
}
_session.Add(product);
}
In manufacturers table I simply have ID and Name columns. The name is set to NOT NULL. Each product has ManufacturerID, which can be NULL. I have added FK relationship from Manufacturer.ID <=> Product.ManufacturerID.
I also have set the default value of Product.ManufacturerID to null using the DefaultValue data annotation with model metadata.
This works fine when I specify some manufacturer when creating a product. But when I leave it blank, an ID gets assigned for Product.ManufacturerID in the database but no Manufacturer is created with that ID. Debugger showed that Product.Manufacturer.Name is null, but for some reason Product.Manufacturer.ID and Product.ManufacturerID are set to 0 (not null). How can I make it so that it will leave Product.ManufacturerID null when no manufacturer is specified?
Would it be a better idea to detach Manufacturer entity and have the action look like this,
public ActionResult Create(Product product, Manufacturer manufacturer) {
// ...
}
I guess relying too much on the model binder is not a good idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了。您需要做的是为产品和制造商创建一组视图模型。查看代码,我假设您的 Product 类是 linq-to-sql 生成的类。
您可能需要考虑添加额外的表单项,例如指示是否要添加制造商的复选框。
预期行为,因为 0 是 int 的默认值(不能为空)
You are almost there. What you need to be doing is creating a set of Viewmodels for the Product and Manufacturer. Looking at the code, I am assuming that your Product class is a linq-to-sql generated class.
You may want to consider adding an additional Form item such as a checkbox that indicates whether you want to add a manufacturer or not.
Expected behaviour since 0 is the default value for an int (which cannot be null)