填写下拉列表来自另一个下拉列表的值

时间:2016-07-15 05:50:00

标签: c# jquery asp.net-mvc drop-down-menu

我有一个issuedate的下拉列表,这些日期通过在http get方法中对其应用了一些过滤器来填充我的下拉列表

ViewBag.PMID1 = new SelectList(db.PRIMKT_MAST.Where(z => z.PRODUCT_ID == j && z.REC_VERIFIED == 0 && z.VERIFY_LOCK == 0 && z.TRTYPE == i), "PMID", "ISSUE_DATE");

这是我的下拉列表。

            @ Html.LabelFor(model => model.ISSUE_DATE,new {@class =" control-label col-md-2"})                                      @ Html.DropDownList(" PMID1",null,new {@onchange =" FillTenor()"})                         @ Html.ValidationMessageFor(model => model.ISSUE_DATE)                  

现在,我想在此下拉列表填写的表格中选择AUCTIONID,例如select auctionid where issuedate = dropdown selected value

当我们获得auctionid时,我想要使用此auctionid并在其他表格中搜索并根据此auctionid填写另一个下拉列表 select Tenors(i want to select this value) where auctionid is (whatever we get from dropdown) 这是我要填写的第二个Dropdownt列表

     <div class="form-group">
            @Html.LabelFor(model => model.TENOR_ID, "TENOR_ID", new { @class     = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(m => m.TENOR_ID,
new SelectList(Enumerable.Empty<SelectListItem>(), "AUCDT_ID", "TENOR_ID"),
              "Select Tenors",
              new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.BRANCH_ID)
            </div>
        </div>

This is my Controller
  public ActionResult Create()
        {
            ViewBag.AUCTION_ID = new SelectList(db.AUCTION_DATA, "AUCTION_ID", "PRODUCT_ID");
            ViewBag.CUSTOMER_ID = new SelectList(db.CUSTOMERs, "CUSTOMER_ID", "CUSTOMER_NAME");
            ViewBag.PMID = new SelectList(db.PRIMKT_MAST, "PMID", "PRODUCT_ID");
            ViewBag.BRANCH_ID = new SelectList(db.BRANCHes, "BRANCH_ID", "BRANCH_DESC");
           //ViewData["ListofIssueDate"] = db.PRIMKT_MAST.Where(z => z.PRODUCT_ID == Session["Product_Id"] && z.REC_VERIFIED == 0 && z.VERIFY_LOCK == 0 && z.TRTYPE == Convert.ToInt32(Session["Trtype"])).Select(z => z.ISSUE_DATE);
            int i = Convert.ToInt32(Session["Trtype"]);
            string j =Convert.ToString( Session["Product_Id"]);
            ViewBag.PMID1 = new SelectList(db.PRIMKT_MAST.Where(z => z.PRODUCT_ID == j && z.REC_VERIFIED == 0 && z.VERIFY_LOCK == 0 && z.TRTYPE == i), "ISSUE_DATE", "ISSUE_DATE");
            return View();
        }

我的Ajax电话..

@section script {
    <script>
        function FillTenor() {
            var IssueDate = $('#PMID1').val();
            $.ajax({
                url: '@Url.Action("FillCity", "PrimaryMarket")',
                type: "GET",
                dataType: "JSON",
                data: { IssueDate: $("#PMID1").val() },
                success: function (Tenor) {
                    $("#TENOR_ID").html(""); // clear before appending new list
                    $.each(Tenor, function (i, AUCDATA_TENORS) {
                        $("#TENOR_ID").append(
                            $('<option></option>').val(AUCDATA_TENORS.AUCDT_ID).html(AUCDATA_TENORS.TENOR_ID));
                    });
                }
            });
        }
    </script>
}


My onchange Method
 [HttpGet]
        public ActionResult FillTenor(DateTime? IssueDate)
        {
            var auctionid = db.PRIMKT_MAST.Where(c => c.ISSUE_DATE == IssueDate).Select(c=>c.AUCTION_ID).ToList();
            var Tenor = db.AUCDATA_TENORS.Where(c => c.AUCTION_ID ==( auctionid[0])).Select(c => c.TENOR_ID);

            return Json(Tenor, JsonRequestBehavior.AllowGet);
        }

Exception

1 个答案:

答案 0 :(得分:0)

乍一看,带有LINQ to Entities does not recognize the method的{​​{1}}意味着您在lambda表达式中有一些可能返回null或空值的数组值,因此无法将其转换为正确的SQL语句来填充您的级联下拉列表。

我怀疑这个表达式产生了异常:

get_Item(Int32)

建议的lambda表达式:

var Tenor = db.AUCDATA_TENORS.Where(c => c.AUCTION_ID ==( auctionid[0])).Select(c => c.TENOR_ID);

注意:此行为也适用于包含属性的类中的数组。

欢迎任何改进和建议。

参考:LINQ to Entities does not recognize the method ... get_Item(Int32)