Save uploaded data from a partialView

Asked

Viewed 129 times

1

I have a PartialView with BeginCollection where I upload a list of products to Dropdowns dynamic:

Partial View

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 ?

1 answer

2


Apparently you’re already using the BeginCollectionItem, then I will answer from the premise that all implementation of it is correct.

By your code, you are passing the navigation property on DropDownList, and not the product id. Change that part to that:

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

Done that you will pass the IdProduct for controller. After that, save normally.

It is worth mentioning that the Scaffolding of Visual Studio, it does not automatically add the list, by default, in Model Biding. Add the list on Bind to work, like this:

public ActionResult Create([Bind(Include = "IDSupplier,Name,Products")] Supplier supplier)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.