Mussaib Siddiqui Mussaib Siddiqui - 4 months ago 17
jQuery Question

Filling Dropdownlist on a value coming from another Dropdown

I have a Dropdown of

issuedate
these dates are filling in my dropdown by applying some filters on it in a http get method

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


This is my dropdownlist.


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



Now i want to select
AUCTIONID
in a table where this dropdownlist is filling on basis of issue date like
select auctionid where issuedate = dropdown selected value


When we get
auctionid
I want to take this
auctionid
and search in other table and fill another dropdown on basis of this
auctionid
like
select Tenors(i want to select this value) where auctionid is (whatever we get from dropdown)

This is my second Dropdownt list which i want to fill

<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();
}


my Ajax Call..

@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

Answer

At first glance, LINQ to Entities does not recognize the method with get_Item(Int32) means that you have certain array value assignment inside lambda expression that potentially returning null or empty value, thus it cannot be translated to proper SQL statement to fill your cascaded drop down list.

I suspected this expression generated that exception:

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

Suggested lambda expression:

// common practice handling array value(s) into lambda:
// use a variable outside lambda to hold array value(s),
// then pass that variable inside the lambda
var auctionId = auctionid[0]; // this may improved to check against null or empty value
var Tenor = db.AUCDATA_TENORS.Where(c => c.AUCTION_ID == auctionId).Select(c => c.TENOR_ID);

NB: This behavior also applies to array from a class containing properties.

Any improvements and suggestions welcome.

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