繁体   English   中英

从下拉列表中选择的项目显示的动态表数据传递到Asp.Net MVC中的控制器

[英]Passing data of dynamic table displayed from dropdownlist selected item to controller in Asp.Net MVC

我有一个dropwonlist,根据所选项目,将显示一个动态表,其中每行包含2个文本框和1个复选框。 填充文本框并选中复选框后,我要将经过检查的行(多于一行)传递给controller。 我需要你的帮助。 谢谢:) 脚本:

<script>
    $(function () {
        $("#ProduitID").change(function () {

            $.get("/Demandes/GetGabarit", { produit: $("#ProduitID").val() }, function (data) {

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

                    $("#Gabarit").append($("<tr>").append("<td>"+"<input type=checkbox name='"+row.Designation+"'/>"+"</td>"
                                                           +"<td>" + row.Designation + "</td>"
                                                        + "<td>" + "<input type=text style=width:50px; />" + "</td>"

                   ));


                });
            })
        });

    });

视图:

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

                    @Html.DropDownListFor(p => p.CodeBarre, ViewBag.Produit as SelectList, "Sélectionner un produit", new { id = "ProduitID", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Produit, "", new { @class = "text-danger" })
                </div>
            </div>

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

                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th width="25%">Check</th>
                                <th width="25%">@Html.DisplayNameFor(model => model.Designation)</th>

                                <th width="25%">@Html.DisplayNameFor(model => model.Quantite)</th>

                            </tr>
                        </thead>
                        <tbody id="Gabarit">
                            <tr>
                                <td class="Designation"></td>

                                <td class="Quantite" name="Qt"></td>
                                <td class="Check"></td>
                            </tr>
                        </tbody>
                    </table>

控制器:

public ActionResult Create( DemandeViewModel demande)
    {

        if (ModelState.IsValid)
        {
            string strQt = Request.Form["Qt"].ToString();
            var dbdemande = new Demande()
            {
                Emetteur = demande.Emetteur,
                id = demande.id,
                Date=DateTime.Now,
                Quantite = Convert.ToInt32(strQt),
                CodeBarre = //i don't know what to put


            };


            demanderepository.Insert(dbdemande);

            return RedirectToAction("Index");
        }
        ViewBag.DesignationAtelier = new SelectList(db.Atelier.Where(c => c.idPole == idPole), "id", "DesignationAtelier",demande.id);

        var Produit = db.Gabarit.Select(x => x.Produit).Distinct();
        ViewBag.Produit = new SelectList(Produit);


        return View(demande);
    }

视图模型:

 public class DemandeViewModel
{
    public int CodeBarre { get; set; }
    public string Emetteur { get; set; }
    public int id { get; set; }
    public Nullable<int> Produit { get; set; }
    public string Designation { get; set; }
    public string Photo { get; set; }
    public int Quantite { get; set; }
    public string DesignationAtelier { get; set; }
    public List<Gabarit> Gabarits { get; set; }
    public Nullable<System.DateTime> Date { get; set; }



}

PS:从产品选择的项目中显示Gabarits。 我在控制器中的代码不起作用。 谢谢

您的方法上是否有[HttpPost]前缀?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM