MVC 4 - 集中创建操作

时间:2013-04-18 09:00:34

标签: c# asp.net-mvc razor asp.net-mvc-4

我会尽力清楚。我正在开发一个基于MVC 4和Entity Framework的Web应用程序。通过这个应用程序,我可以创建一些产品,这取决于另一个表产品类型正如您所看到的,在我的创建产品视图中,我有一个下拉列表包含产品类型:

@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>
}

在我的创建产品类型View中,我有一个现有产品公司的下拉列表(因此产品和产品类型之间存在相同的关系:

@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>
}

我尝试的是在一个创建产品视图中“混合”这两个视图。所以我认为我的行动会有所改变。另外,我想我必须在我的数据库中添加2个。这是做我想做的最好的方式吗?

更新:通过使用viewmodel,我得到了这个:

我的视图模型:

public class ProductViewModel
{
    [Required]
    public Product Product { get; set; }
    [Required]
    public ProductType ProductType { get; set; }
}

我的创建动作:

[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);
}

当我尝试保存新条目时,我遇到了这个问题:The INSERT statement conflicted with the FOREIGN KEY constraint "FK_bm_ProductTypes_bm_ProductCompanies".

1 个答案:

答案 0 :(得分:1)

ProductsProductTypes为“依赖”,将它们合并为一个是个好主意。您还必须合并帖子操作,这将有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)