Save data loaded from a partialView

1

I have PartialView with BeginCollection where I load a list of products in Dropdowns dynamic:

InthisprojectIhaveaManyToManyrelationbetweenProductandVendorwhereIhavetheProdutosFornecedoresentity.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 ?

    
asked by anonymous 22.06.2016 / 21:33

1 answer

2

Apparently you're already using BeginCollectionItem , so I'll respond from the premise that the whole implementation of it is correct.

By your code, you're passing the navigation property on DropDownList , not the product id. Change this part to this:

 @Html.DropDownListFor(x => x.IdProduct, new SelectList(ViewBag.Products, "IdProduct", "Name"), new { @class = "form-control" })

Once you have done this you will pass IdProduct to controller. After that, save normally.

Note that if you are using Scaffolding of Visual Studio, it does not automatically add the list, by default, to Model Biding . Add list in Bind to work, type like this:

public ActionResult Create([Bind(Include = "IDSupplier,Name,Products")] Supplier supplier)
    
23.06.2016 / 19:37