I am actually working on an ASP.NET MVC4 application where we used the entity infrastructure and Model-View-View-Model approach and 7-layer architecture. We have one page where we need to insert or update product information. Entered or updated product data will be saved in the Product table. My database name is "DbOnix". The basic structure of the product table is as follows:
Column Name Data Type Allow Nulls
ProductID PK int
ProductName varchar(255) NO
ProductCategoryID FK int
Sequence int YES
ActiveStatus int YES
SlNo int NO
The ProductCategoryID column in the Product table has a foreign key relationship to the ProductCategory table. The basic structure of the ProductCategory table:
Column Name Data Type Allow Nulls
ProductCategoryID PK int
ProductCategoryName varchar(150) NO
Whenever I try to insert or update data in the Product table, the following exception is thrown:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Product_ProductCategory". The conflict occurred in database "DbOnix", table "dbo.ProductCategory", column 'ProductCategoryID'.The statement has been terminated.
My controller code:
public HttpStatusCodeResult UpdateProductInformation(int id, ProductDTO ProductDTO)
{
_productManager.UpdateProductInformation(id, ProductDTO);
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
My manager class code is:
public void UpdateProductInformation(int id, ProductDTO productDTO)
{
if (productDTO == null)
throw new ArgumentException(Messages.warning_CannotAddProfileWithNullInformation);
var currentProduct = _ProductRepository.Get(id);
var updatedProduct = new Product();
updatedProduct.ProductID = id;
updatedProduct.ProductName = productDTO.ProductName;
updatedProduct.ProductCategoryID = productDTO.ProductCategoryID;
updatedProduct.Sequence = productDTO.Sequence;
updatedProduct.ActiveStatus = productDTO.ActiveStatus;
updatedProduct.SlNo = productDTO.SlNo;
updatedProduct = this.UpdateProduct(currentProduct, updatedProduct);
}
My Core (Property) Class Code:
public partial class Product : Entity, IValidatableObject
{
public Product()
{
}
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID { get; set; }
public int Sequence { get; set; }
public int ActiveStatus { get; set; }
public int SlNo { get; set; }
}
DTO:
public class ProductDTO
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID { get; set; }
public int Sequence { get; set; }
public int ActiveStatus { get; set; }
public int SlNo { get; set; }
}
, - MS SQL Server 2008 R2.
EDIT-1: Javascript:
$(function () {
var Product = function (Product) {
var self = this;
self.ProductID = ko.observable(Product ? Product.ProductID : 0).extend({ required: true });
self.ProductName = ko.observable(Product ? Product.ProductName : '').extend({ required: true });
self.ActiveStatus = ko.observable(Product ? Product.ActiveStatus : 0);
};
var ProductCollection = function () {
var self = this;
if (ProductID == 0) {
self.Product = ko.observable(new Product());
}
else {
$.ajax({
url: urlProduct + '/GetProductById/' + ProductID,
async: false,
dataType: 'json',
success: function (json) {
self.Product = ko.observable(new Product(json));
}
});
}
self.ProductErrors = ko.validation.group(self.Product());
self.saveProduct = function () {
var isValid = true;
if (self.ProductErrors().length != 0) {
self.ProductErrors.showAllMessages();
isValid = false;
}
if (isValid) {
self.Product().ActiveStatus = document.getElementById("stat").value;
$.ajax({
type: (ProductID > 0 ? 'PUT' : 'POST'),
cache: false,
dataType: 'json',
url: urlProduct + (ProductID > 0 ? '/UpdateProductInformation?id=' + ProductID : '/SaveProductInformation'),
data: JSON.stringify(ko.toJS(self.Product())),
contentType: 'application/json; charset=utf-8',
async: false,
success: function (data) {
alert("Product saved successfully.");
window.location.href = '/Product';
},
error: function (err) {
var err = JSON.parse(err.responseText);
var errors = "";
for (var key in err) {
if (err.hasOwnProperty(key)) {
errors += key.replace("Product.", "") + " : " + err[key];
}
}
$("<div></div>").html(errors).dialog({ modal: true, title: JSON.parse(err.responseText).Message, buttons: { "Ok": function () { $(this).dialog("close"); } } }).show();
},
complete: function () {
}
});
}
};
};
var ProductsViewModel = function () {
var self = this;
var url = "/Product/GetAllProduct";
var refresh = function () {
$.getJSON(url, {}, function (data) {
self.Products(data);
});
};
self.Products = ko.observableArray([]);
self.createProduct = function () {
window.location.href = '/Product/ProductCreateEdit/0';
};
self.editProduct = function (product) {
window.location.href = '/Product/ProductCreateEdit/' + product.ProductID;
};
};
ko.applyBindings(new ProductsViewModel(), document.getElementById("productlist"));
ko.applyBindings(new ProductCollection(), document.getElementById("product_edit"));
});
, KnockoutJS v2.3.0