1
I have a PartialView
with BeginCollection
where I upload a list of products to Dropdowns
dynamic:
In this project I have a relationship ManyToMany
between Product and Supplier where I have the entity ProdutosFornecedores
. I would like to save the Supplier also save the data in that entity which is the relationship between the other two.
In my Controller
of suppliers have the following method:
public ActionResult getProducts()
{
ViewBag.Products = db.Products.ToList();
return PartialView("_Product", new ProductsSuppliers());
}
The Partial is that way:
@model CodingCraft01.Models.ProductsSuppliers
@using (Html.BeginCollectionItem("ProductsSuppliers"))
{
<div id="productRow" class="productRow">
<div class="row">
<div class="col-md-8">
@Html.DropDownListFor(x => x.Product, new SelectList(ViewBag.Products, "IdProduct", "Name"), new { @class = "form-control" })
</div>
<div class="col-md-4">
@Html.EditorFor(x => x.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(x => x.Price, "", new { @class = "text-danger" })
</div>
</div>
<a href="#" id="deleteRow" class="deleteRow" onclick="$(this).parent().remove();">Delete</a>
</div>
}
And in Suppliers View:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Supplier</h4>
<hr />
<div class="row">
<div class="col-md-4">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="col-md-1">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<a class="btn btn-primary" id="btnAddProduct" href="javascript:void(0);">Add Product</a>
</div>
</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-md-4">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="form-group" id="new-product"></div>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
What do I need to do to save the data from Fornecedor
and the entity ProdutosFornecedores
?