循环只是插入最后一行



我是C#的新手,我正在编写一个网站,需要在数据库中的一行中插入多个数据。一些数据将自动设置,另一些则来自表单。这是我的控制器部分:

public ActionResult chekout()
{
return View(new Commande());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult chekout([Bind(Include = "Id_commande,prenom,nom,Adresse,telephone,Email,produit,prix,cart")] Commande commande)
{
var panier = from Panier in db.Paniers
select Panier;
var userEmail = this.User.Identity.Name;
panier = panier.Where(x => x.user.Contains(userEmail));
Panier[] pa = panier.ToArray();
List<Commande> list = new List<Commande>();
for (int i = 0; i < pa.Length; i++)
{
commande.Id_commande = db.Commandes.Max(I => I.Id_commande) + 1;
commande.produit = pa[i].Quantite + " X " + pa[i].nom_produit;
commande.prix = int.Parse(pa[i].prix) * pa[i].Quantite;
commande.Email = userEmail;
list.Add(commande);
}
db.Commandes.AddRange(list);
db.SaveChanges();
return RedirectToAction("order_complete", "Home");
}

这是我的观点部分:

@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.prenom, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.prenom, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.prenom, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.nom, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.nom, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.nom, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Adresse, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Adresse, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.Adresse, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.telephone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.telephone, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.telephone, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.cart, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cart, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.cart, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type = "submit" value="Commander" class="btn btn-primary" />
</div>
</div>
}

我没有得到错误,但我不知道为什么,但它只是添加了最后一行所以如果有人能帮助我,我将不胜感激。

如果在循环中重复添加相同的commande对象,则需要在每次迭代中实例化新的对象命令。您需要在下面的循环中添加第一行,如果类级别存在相同的名称,请更改变量名称"command"。

for (int i = 0; i < pa.Length; i++)
{
Commande commande = new Commande ();
commande.Id_commande = db.Commandes.Max(I => I.Id_commande) + 1;
commande.produit = pa[i].Quantite + " X " + pa[i].nom_produit;
commande.prix = int.Parse(pa[i].prix) * pa[i].Quantite;
commande.Email = userEmail;
list.Add(commande);
}

最新更新