我是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>
}
我没有收到错误,但我不知道为什么,但是它只是添加了最后一行 因此,如果有人可以帮助我,我将不胜感激。
答案 0 :(得分:0)
您要在循环中重复添加相同的commande
对象,您需要在每次迭代中实例化新的对象命令。您需要在下面的循环中添加第一行,如果在类级别存在相同的名称,请务必更改变量名称“ commande”。
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);
}