In my application, from the view I assigned a combo box to select the supplier from the list. And then the selected ID I'm sending to the JasonResult and there I did this,
public JsonResult AutocompleteSupplierFromName(int SupplierID)
{
var Supplier = db.Suppliers.Where(x => x.ID == SupplierID).ToList();
if (Supplier.Count() == 0) {
return Json(new {
Success = false
});
} else {
ViewBag.Item_ID = new SelectList(db.Items.Where(x => x.Status == true && x.Supplier_Id == SupplierID), "ID", "ItemName_Number");
return Json(new {
Success = true, supName = Supplier.ToList().First().Supplier_Name, supID = Supplier.ToList().First().ID, supAddress01 = Supplier.ToList().First().Supplier_Address_Line01, supAddress02 = Supplier.ToList().First().Supplier_Address_Line02, supContact = Supplier.ToList().First().ContactNumber
});
}
}
I want to know, from this Can I load the ViewBag.Item_ID to the view bag ??
Because when I checked the ViewBag.Item_ID from the view, It returns null.
This is the full code
var dropdownOptions;
try {
$.ajax({
url: rootDir + 'Ajax/AutocompleteSupplierFromName',
type: 'POST',
dataType: 'json',
cache: false,
async: false,
data: {
SupplierID: supID
},
success: function (data) {
console.log(data);
if (data.Success == true) {
document.getElementById('AddressLine01').value = data.supAddress01;
document.getElementById('AddressLine02').value = data.supAddress02;
document.getElementById('ContactNumber').value = data.supContact;
document.getElementById('Supplier_Name').value = data.supName;
$(".SupplierIDClass").val(data.supID);
dropdownOptions = @Html.Raw(Json.Encode(ViewBag.Item_ID));
console.log(dropdownOptions);
} else {
alert(data.userName);
}
}
});
} catch (e) {
}
Edited
I changed the jsonresult to
public JsonResult AutocompleteSupplierFromName(int SupplierID)
{
var Supplier = db.Suppliers.Where(x => x.ID == SupplierID).ToList();
if (Supplier.Count() == 0) {
return Json(new {
Success = false
});
} else {
DropDownJasonData data = new DropDownJasonData();
data.Results = GetSupplierItem(SupplierID);
data.Pagination = new Pagination {
More = true
};
string JSONresult = Newtonsoft.Json.JsonConvert.SerializeObject(data.Results);
return Json(new {
Success = true,
items = JSONresult,
supName = Supplier.ToList().First().Supplier_Name,
supID = Supplier.ToList().First().ID,
supAddress01 = Supplier.ToList().First().Supplier_Address_Line01,
supAddress02 = Supplier.ToList().First().Supplier_Address_Line02,
supContact = Supplier.ToList().First().ContactNumber
}, JsonRequestBehavior.AllowGet);
}
}
private List < Result > GetSupplierItem(int SupID)
{
List < SelectListItem > Items = (from i in db.Items where i.Supplier_Id == SupID && i.Status == true select new SelectListItem {
Text = i.ItemName_Number, Value = i.ID.ToString()
}).ToList();
List < Result > dataList = new List < Result > ();
if (Items.Count != 0) {
foreach(var record in Items) {
dataList.Add(new Result {
Text = record.Text, Id = int.Parse(record.Value)
});
}
}
return dataList;
}
So then I'm trying to put the data into the dropdownOptions. Here is my try and I got an error here. Can someone correct me here.?
function getSupDt(supID) {
try {
$.ajax({
url: rootDir + 'Ajax/AutocompleteSupplierFromName',
type: 'POST',
dataType: 'json',
cache: false,
async: false,
data: { SupplierID: supID },
success: function (data) {
if (data.Success == true) {
document.getElementById('AddressLine01').value = data.supAddress01;
document.getElementById('AddressLine02').value = data.supAddress02;
document.getElementById('ContactNumber').value = data.supContact;
document.getElementById('Supplier_Name').value = data.supName;
$(".SupplierIDClass").val(data.supID);
console.log(data.items);
for (var i = 0; i < data.items; i++) {
dropdownOptions = [
{
value: data.items[i].id,
text: data.items[i].text
}
]
}
console.log(dropdownOptions);
}
else {
alert(data.userName);
}
}
});
} catch (e) {
}
}