您好,我正在使用MVC开发一个网站,以便将数据插入到我的数据库中,我在chekout
中创建了一种名为HomeController
的方法,如下所示:
public ActionResult chekout()
{
return View(Tuple.Create<Commande,IEnumerable< Panier >, LoginViewModel> (new Commande(), db.Paniers.ToList(), new LoginViewModel()));
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult chekout([Bind(Include = "prenom,nom,Adresse,telephone,cart")] Commande commande)
{
if (ModelState.IsValid)
{
try
{
commande.Id_commande = db.Commandes.Max().Id_commande + 1;
var panier = from Panier in db.Paniers
select Panier;
var userEmail = this.User.Identity.Name;
panier = panier.Where(x => x.user.Contains(userEmail));
foreach (var item in panier)
{
commande.produit = item.Quantite + " X " + item.nom_produit;
commande.prix = int.Parse(item.prix) * item.Quantite;
commande.Email = userEmail;
db.Commandes.Add(commande);
db.SaveChanges();
}
return RedirectToAction("order_complete", "Home");
}
catch (DbEntityValidationException ex)
{
foreach (var entityValidationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in entityValidationErrors.ValidationErrors)
{
Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
Console.ReadKey();
}
}
return RedirectToAction("chekout", "Home");
}
}
然后我像这样在视图chekout
中创建表单:
@using (Html.BeginForm("chekout", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Item1.prenom, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item1.prenom, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.Item1.prenom, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Item1.nom, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item1.nom, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.Item1.nom, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Item1.Adresse, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item1.Adresse, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.Item1.Adresse, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Item1.telephone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item1.telephone, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.Item1.telephone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Item1.cart, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Item1.cart, new { htmlAttributes = new { @class = "form-control", required = "required", title = "veuillez remplir ce champ" } })
@Html.ValidationMessageFor(model => model.Item1.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>
}
这是我插入数据后的页面,我单击Commander,它是想将数据插入数据库,然后在order_complete页面上移动,但它不是,它会擦除我的数据并重定向到同一页面
但是它不起作用,我不知道为什么当我单击按钮时,它带我返回到同一页面,而没有在数据库中插入数据 如果有人可以帮助我,我将不胜感激