I have PartialView
with BeginCollection
where I load a list of products in Dropdowns
dynamic:
InthisprojectIhaveaManyToMany
relationbetweenProductandVendorwhereIhavetheProdutosFornecedores
entity.IwouldliketosavetheSupplieralsosavethedatainthatentitywhichistherelationshipbetweentheothertwo.
Inmy%ofvendors%Ihavethefollowingmethod:
publicActionResultgetProducts(){ViewBag.Products=db.Products.ToList();returnPartialView("_Product", new ProductsSuppliers());
}
Partial looks like this:
@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 the 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 of Controller
and entity Fornecedor
?