0

您好,我正在使用 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>
}

在此处输入图像描述 这是我插入数据之后的页面 在此处输入图像描述

但它没有用,我不知道为什么当我单击按钮时,它会将我重定向到同一页面而不将数据插入到我的数据库中,如果有人可以帮助我,我将不胜感激

4

0 回答 0