我如何使用元组将数据插入数据库

时间:2018-12-03 18:31:06

标签: c# asp.net-mvc

嗨,我在视图(chekout)中使用2个模型,因为我使用了如下这样的元组:

@model Tuple<storApp.Models.Commande, IEnumerable<storApp.Models.Panier>, storApp.Models.LoginViewModel>

我想将数据插入我创建的Commande模型中,如下所示:

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

这是我的控制器部分:

 public ActionResult chekout()
        {

            return View(Tuple.Create<Commande,IEnumerable< Panier >, 
LoginViewModel> (new Commande(), db.Paniers.ToList(), new LoginViewModel()));

        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult chekout([Bind(Prefix = "Item1")]Commande commande)
        {
            var errors = ModelState
                .Where(x => x.Value.Errors.Count > 0)
                .Select(x => new { x.Key, x.Value.Errors })
                .ToArray();

            if (ModelState.IsValid)
            {

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

            }else return RedirectToAction("shop", "Home");



        }

但这对我不起作用,我没有错误,没有什么ModelState.IsValid是错误的,我可以弄清楚为什么 如果有人可以帮助我,我将不胜感激

0 个答案:

没有答案