mardi 28 mars 2017

Passing data from one form to another in asp.net mvc

I want to pass data from one form to another. I have a form of products and after submitting this form i want to add buttom to add posts to for this product, so i want to pass the Product ID to the other form.

this is my product.cs:

public partial class Produits
{

    public Produits()
    {
        this.Postes = new HashSet<Postes>();
    }

    public int idProduit { get; set; }
    public int idPole { get; set; }
    public Nullable<int> Reference { get; set; }
    public string DesignationProduit { get; set; }

    public virtual Pole Pole { get; set; }

    public virtual ICollection<Postes> Postes { get; set; }
}

This is my Post.cs:

public partial class Postes
{
    public int idPoste { get; set; }
    public Nullable<int> Numero { get; set; }
    public int idProduit { get; set; }
    public int idAtelier { get; set; }
    public string DesignationPoste { get; set; }

    public virtual Atelier Atelier { get; set; }
    public virtual Produits Produits { get; set; }
}

this is my Create.cshtml for the product:

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Produits</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })


    <div class="form-group">
        @Html.LabelFor(model => model.idPole, "Pole", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("idPole", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.idPole, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Reference, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Reference, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Reference, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DesignationProduit, htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.EditorFor(model => model.DesignationProduit, new {htmlAttributes = new {@class = "form-control"}})
            @Html.ValidationMessageFor(model => model.DesignationProduit, "", new {@class = "text-danger"})
        </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>
}

and this my Create.cshtml for the Post:

<form asp-controller="Postes" asp-action="Create" method="post" id="FormPs" role="form">
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Postes</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })

    <div class="form-group">
        @Html.LabelFor(model => model.idProduit, "Produit", htmlAttributes: new {@class = "control-label col-md-2"})
        <div class="col-md-10">
            @Html.DropDownList("idProduit", null, htmlAttributes: new {@class = "form-control"})
            @Html.ValidationMessageFor(model => model.idAtelier, "", new {@class = "text-danger"})
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.idAtelier, "Atelier", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("idAtelier", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.idAtelier, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DesignationPoste, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DesignationPoste, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DesignationPoste, "", new { @class = "text-danger" })
        </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>
 }

My Product controller:

 public ActionResult Create([Bind(Include = "idProduit,idPole,Reference,DesignationProduit")] Produits produits)
    {
        if (ModelState.IsValid)
        {
            db.Produits.Add(produits);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.idPole = new SelectList(db.Pole, "id", "designation", produits.idPole);
        return View(produits);
    }

My Post Controller:

 public ActionResult Create(Postes postes)
    {
        if (ModelState.IsValid)
        {
            db.Postes.Add(postes);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.idAtelier = new SelectList(db.Atelier, "id", "DesignationAtelier", postes.idAtelier);
        ViewBag.idProduit = new SelectList(db.Produits, "idProduit", "DesignationProduit", postes.idProduit);
        return View();
    }

I want to pass idProduit to the other form, Idproduit is an not manually added.

Aucun commentaire:

Enregistrer un commentaire