1

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".

4

1 回答 1

1

作为Products依赖ProductTypes,将它们合并为一个是个好主意。您还必须合并 post 操作,这将有 2 个插入到您的数据库中(这是正确的,一个用于Product,一个用于ProductType.

您还必须将它们都放在一个模型中,以便您可以在视图中使用它,例如:

public class ProductViewModel
{
    public Product Product { get; set; }
    public ProductType ProductType { get; set; }
}

编辑:您的储蓄问题是因为没有发布 ProductComany(如聊天中所示)

为了解决这个问题,首先我们将 Dropdown 的值放入模型中:

public class ProductViewModel
{
    public Product Product { get; set; }
    public ProductType ProductType { get; set; }
    public List<SelectListItem> ProductCompanies { get; set; }
}

然后通过执行以下操作将其填充到您的HttpGetHttpPost中:

model.ProductCompanies = db.ProductCompanies
    .ToList()
    .Select(s => new SelectListItem
    {
        Text = s.Name,
        Value = s.Id.ToString()
    })
    .ToList();

然后在您看来,您可以执行以下操作:

@Html.DropDownListFor(m => m.ProductType.Id_ProductCompany, Model.ProductCompanies)
于 2013-04-18T09:04:10.823 回答