asp.net mvc中的级联下拉列表:在asp.net mvc中的数据库中插入选定的值

时间:2017-03-31 14:50:46

标签: asp.net-mvc

我必须显示另一个下拉列表中的下拉列表,这对我有用,但问题是当我在数据库中插入选定的值时,它没有插入产品的参考值或后缀的值数字,它插入productId和postId。 我不知道如何解决这个问题。 的控制器:

   public ActionResult Create()
    {
        ViewBag.ProduitList = new SelectList(db.Produits, "idProduit", "Reference");
        return View();
    }

    // POST: Gabarits/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(GabaritViewModel gabarit, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            string strPoste = Request["poste"].ToString();
            var uploadDir = "~/Images";
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(uploadDir, fileName);

            //file.SaveAs(path);

            var reference = gabarit.Reference.ToString();
            var poste = gabarit.Numero.ToString();
            var CB = (reference + strPoste + gabarit.Exemplaire);
            int CodeB = Convert.ToInt32(CB);

            var dbgabarit = new Gabarit()
            {
                Designation = gabarit.Designation,
                Produit = gabarit.idProduit,
                Photo = path,
                Poste = Convert.ToInt32(strPoste),
                Exemplaire = gabarit.Exemplaire,
                CodeBarre = CodeB
            };

           gabaritrepository.Insert(dbgabarit);
            //return RedirectToAction("Index");
        }

        ViewBag.ProduitList = new SelectList(db.Produits, "idProduit", "Reference",gabarit.idProduit);
        ViewBag.PosteList = new SelectList(db.Postes, "idPoste", "Numero", gabarit.idPoste);

        return View(gabarit);
    }

    public JsonResult ChoisirPoste(int ID)
    {
        db.Configuration.ProxyCreationEnabled = false;

        return Json(db.Postes.Where(p => p.idProduit == ID), JsonRequestBehavior.AllowGet);
    }

查看:

<script>
//script for cascading dropdownlists: Produit=>Poste
$(function() {
    $("#IDProduit").change(function() {

        $.get("/Gabarits/ChoisirPoste", { ID: $("#IDProduit").val() }, function (data) {

            $("#IDPoste").empty();
            $.each(data, function(index, row) {

                $("#IDPoste").append(" <option value='" + row.idPoste + "'>" + row.Numero + "</option>");
            });
        })
    });
});

创建

@using (Html.BeginForm("Create", "Gabarits", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Gabarit</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Photo, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="editor-field">
                @Html.TextBox("file", "", new { type = "file" })
            </div>
            </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Reference, "Produit", htmlAttributes: new {@class = "control-label col-md-2"})
            <div class="col-md-10">

                @Html.DropDownListFor(p=>p.idProduit, ViewBag.ProduitList as SelectList, htmlAttributes: new {id = "IDProduit", @class = "form-control"})
                @Html.ValidationMessageFor(model => model.Reference, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Numero, "Poste", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <select id="IDPoste" class="form-control" name="poste"></select>

                @Html.ValidationMessageFor(model => model.Numero, "", new { @class = "text-danger" })

            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Exemplaire, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Exemplaire, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Exemplaire, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

0 个答案:

没有答案