在asp.net mvc中将数据从一种形式传递到另一种形式

时间:2017-03-28 14:17:52

标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

我想将数据从一种形式传递到另一种形式。 我有一种产品形式,在提交此表单后,我想添加buttom为此产品添加帖子,所以我想将产品ID传递给其他表单。

这是我的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; }
}

这是我的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; }
}

这是我对产品的Create.cshtml:

@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>
}

这是我的帖子的Create.cshtml:

<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>
 }

我的产品控制器:

 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);
    }

我的帖子控制器:

 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();
    }

我想将idProduit传递给另一个表单,Idproduit是一个非手动添加的。

0 个答案:

没有答案