New to ASP.NET Core MVC: Trying to bind the result of a sql query (on my Controller) and added to a ViewBag, to an Html dropdown (dropdown B). The Action method is triggered based on a change in another dropdown (dropdown A) using Ajax.
View (if change in dropdown A):
$.ajax({
type: "POST",
url: "/Home/GetBrandsForDropdown",
data: null,
success: function (result) {
alert('Success')
},
error: function (req, status, error) {
alert('No Success')
}
});
Note: Alert is 'No Success" when I run it.
Controller:
public IActionResult GetBrandsForDropdown()
{
var content = from b in Context.Brands
select new { b.BrandNumber, b.BrandName };
ViewBag.BrandListing = content.Select(c => new SelectListItem
{
Text = c.BrandName,
Value = c.BrandNumber.ToString()
}).ToList();
return View();
}
Note: ViewBag.BrandListing includes values when I debug.
Trying but unsuccessful to get the values in the ViewBag into the following:
<select class="dropdown form-control" onchange="getOption()" name="brandDDL"
id="ddlBrandName"style="background-color: #E6E6E6;
font-size: small; color: black; font-weight: bold;
border: double">
This seems to be something that should be easily solved but I'm having no luck in getting my desired result.
Any/All help would be very much appreciated. Jack
