I have a Product Model, which contains a foreign key to a ProductCategory record, and looks like this.
public class ProductModel : IProductModel
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public string ProductImagePath { get; set; }
public decimal? PricePerMonth { get; set; }
public bool ProductActive { get; set; }
public ProductCategory ProductCategory { get; set; }
}
For some reason, every time a new Product is created, a new ProductCategory is also created with all of it's values in the database being set to NULL, except for the ProductCategoryID field, which is auto generated.
In my Create view, I have a form, which contains a drop list populated with ProductCategories. Once the form is submitted, Create(Product product) Action is being called, and the new Product is being created. At this point, the new ProductCategory is also created, which is behavior that I don't want.
My Create(Product product) Action looks like this:
//
// POST: /Product/Create
[HttpPost]
public ActionResult Create(Product product)
{
int productCategoryID = product.ProductCategory.ProductCategoryID;
product.ProductCategoryID = productCategoryID;
_productService.InsertProduct(product);
}
... and my InsertProduct methods looks like this ...
public void InsertProduct(Product product)
{
Db.Products.AddObject(product);
Save();
}
Why is this code causing a new ProductCategory to be created every time?
EDIT: Adding table definition, in response to an answer given, whcih asked for more information:
TABLE Product (
ProductID INT PRIMARY KEY IDENTITY(1,1),
ProductCategoryID INT, -- FK to ProductCategory
ProductName NVARCHAR(255),
ProductDescription NVARCHAR(MAX),
ProductImagePath NVARCHAR(1024),
PricePerMonth DECIMAL(7,2), -- ex 11111.11
ProductActive BIT NOT NULL DEFAULT(1)
)
TABLE ProductCategory (
ProductCategoryID INT PRIMARY KEY IDENTITY(1,1),
ProductCategoryName NVARCHAR(255),
ProductCategoryDescription NVARCHAR(MAX),
ProductCategoryActive BIT NOT NULL DEFAULT(1)
)
EDIT 2:
I still don't understand why, exactly, but I was able to work around this problem with the following changes made to my Create(Product product) Action:
var productCategoryID = product.ProductCategory.ProductCategoryID;
product.ProductCategoryID = productCategoryID;
var newProduct = new Product
{
ProductName = product.ProductName,
ProductDescription = product.ProductDescription,
PricePerMonth = product.PricePerMonth,
ProductImagePath = product.ProductImagePath,
ProductCategoryID = productCategoryID
};
_productService.InsertProduct(newProduct);
EDIT 3:
I'm still trying to wrap my head around all of this, but ... I have discovered that this problem has something to do with the ProductCategory member of the Product class. From what I can tell, ModelBinding is automatically populating that property with a new ProductCategory every time the form is submitted. I can consistently avoid the problem I've been having here by explicitly setting Product.ProductCategory = null;