I'll try to be clear. I'm developing a web application based on MVC 4 and Entity Framework. Through this app, I can create some Products which is depending on an other table which is Product Types As you can see, in my create product View, I have a dropdownlist which contains the product types :
@model BuSIMaterial.Models.Product
@{
ViewBag.Title = "Create";
}
<h2>
Create</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Product</legend>
<div class="editor-label">
Purchase date :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.PurchaseDate, new { @class = "datepicker"})
@Html.ValidationMessageFor(model => model.PurchaseDate)
</div>
<div class="editor-label">
Serial number :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.SerialNumber, new { maxlength = 50 })
@Html.ValidationMessageFor(model => model.SerialNumber)
</div>
<div class="editor-label">
Product type :
</div>
<div class="editor-field">
@Html.DropDownList("Id_ProductType", String.Empty)<a href="../ProductType/Create">Add
a new product type?</a>
@Html.ValidationMessageFor(model => model.Id_ProductType)
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Create</button>
</div>
</fieldset>
}
And in my create product type View, I have a dropdownlist of existing product companies (so the same relation which exists between product and product type :
@model BuSIMaterial.Models.ProductType
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductType</legend>
<div class="editor-label">
Model :
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Model, new { maxlength = 50 })
@Html.ValidationMessageFor(model => model.Model)
</div>
<div class="editor-label">
Catalog Price :
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CatalogPrice)
@Html.ValidationMessageFor(model => model.CatalogPrice)
</div>
<div class="editor-label">
Company :
</div>
<div class="editor-field">
@Html.DropDownList("Id_ProductCompany", String.Empty)<a href ="../ProductCompany/Create" >Add a new company?</a>
@Html.ValidationMessageFor(model => model.Id_ProductCompany)
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Create</button>
</div>
</fieldset>
}
What I tried is to "mix" these 2 views in one Create Product View. So I think my action will change a little bit. Also, I think I'll have to do 2 adds in my database. Is it the best way to do what I want?
UPDATE : by using a viewmodel, I got this :
My View Model :
public class ProductViewModel
{
[Required]
public Product Product { get; set; }
[Required]
public ProductType ProductType { get; set; }
}
My Create Action :
[HttpPost]
public ActionResult CreateFullProduct(ProductViewModel pvm)
{
ViewBag.Id_ProductCompany = new SelectList(db.ProductCompanies, "Id_ProductCompany", "Name", pvm.ProductType.Id_ProductCompany);
if (ModelState.IsValid)
{
Product product = new Product { PurchaseDate = pvm.Product.PurchaseDate,
SerialNumber = pvm.Product.SerialNumber,
Id_ProductType = pvm.ProductType.Id_ProductType};
ProductType productType = new ProductType {Model = pvm.ProductType.Model,
CatalogPrice = pvm.ProductType.CatalogPrice,
Id_ProductCompany = pvm.ProductType.Id_ProductCompany};
db.ProductTypes.AddObject(productType);
db.Products.AddObject(product);
db.SaveChanges();
return RedirectToAction("Index", "Person");
}
return View(pvm);
}
When I try to save the new entries, I got this issue : The INSERT statement conflicted with the FOREIGN KEY constraint "FK_bm_ProductTypes_bm_ProductCompanies".