Controller:
// GET: Equipments/Create public IActionResult Create() { ViewData["ConditionID"] = new SelectList(_context.Conditions.OrderBy(e => e.EqCondition), "ConditionID", "EqCondition"); ViewData["EqTypeID"] = new SelectList(_context.EqTypes.OrderBy(e => e.EquipmentType), "EqTypeID", "EquipmentType"); ViewData["MakeID"] = new SelectList(_context.Makes.OrderBy(e => e.EqMake), "MakeID", "EqMake"); ViewData["WaterBodyID"] = new SelectList(_context.WaterBodys.OrderBy(e => e.WBName), "WaterBodyID", "WBName"); ViewData["SiteID"] = new SelectList(_context.Sites.OrderBy(e => e.SiteName), "SiteID", "SiteName"); return View(); } // POST: Equipments/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create( [Bind("ConditionID,EqTypeID,EstimateRemainingLife,InstallDate,LastServiceDate,MakeID,Model,Notes,Recommended,ReplacementCost,SerialNumber,WaterBodyID")] Equipment equipment) { try { if (ModelState.IsValid) { _context.Add(equipment); await _context.SaveChangesAsync(); return RedirectToAction("Index"); } } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log. ModelState.AddModelError("", "Unable to save changes. " +"Try again, and if the problem persists " +"Danger!! Will Robertson Danger!!!!!!!!!!!"); } ViewData["ConditionID"] = new SelectList(_context.Conditions.OrderBy(e => e.EqCondition), "ConditionID", "EqCondition", equipment.ConditionID); ViewData["EqTypeID"] = new SelectList(_context.EqTypes.OrderBy(e => e.EquipmentType), "EqTypeID", "EquipmentType", equipment.EqTypeID); ViewData["MakeID"] = new SelectList(_context.Makes.OrderBy(e => e.EqMake), "MakeID", "EqMake", equipment.MakeID); ViewData["WaterBodyID"] = new SelectList(_context.WaterBodys.OrderBy(e => e.WBName), "WaterBodyID", "WBName", equipment.WaterBodyID); ViewData["SiteID"] = new SelectList(_context.Sites.OrderBy(e => e.SiteName), "SiteID", "SiteName", equipment.Waterbody.SiteID); return View(equipment); } public IActionResult GetWaterBody(int siteID) { var waterBody = new List<WaterBody>(); waterBody = getWaterBodyFromDataBaseBySiteID(siteID); return Json(waterBody); } public List<WaterBody> getWaterBodyFromDataBaseBySiteID(int siteID) { return _context.WaterBodys.Where(c => c.SiteID == siteID).OrderBy(w => w.WBName).ToList(); }
JavaScript file I have made copying an online sample:
$(document).ready(function () {$("#site-target").on("change", function () {$list = $("#wb-target"); alert("here")$.ajax({ url: "/GetWaterBody", type: "GET", data: { id: $("#site-target").val() }, //id of the site which is used to extract waterbodies traditional: true, success: function (result) {$list.empty();$.each(result, function (i, item) { alert(item); // $list.append('<option value="' + item["WaterBodyId"] + '"> ' + item["WBName"] + ' </option>'); }); }, error: function () { //alert(xhr.responseText); alert("Danger!! Will Robertson Danger!!!!!!!!!!!"); } }); }); });
How I am referencing in view maybe this is wrong?
<div class="form-group"><label asp-for="Waterbody.SiteID" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="Waterbody.SiteID" class="form-control" asp-items="ViewBag.SiteID"><option>---Select Site First---</option></select></div></div><div class="form-group"><label asp-for="WaterBodyID" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="WaterBodyID" class="form-control" asp-items="ViewBag.WaterBodyID"><option>---Select Site First---</option></select></div></div><div class="form-group"><label asp-for="EqTypeID" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="EqTypeID" class="form-control" asp-items="ViewBag.EqTypeID"><option>---Select Equipment Type---</option></select></div></div><div class="form-group"><label asp-for="MakeID" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="MakeID" class="form-control" asp-items="ViewBag.MakeID"><option>---Select Equipment Make---</option></select></div></div><div class="form-group"><label asp-for="Model" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Model" class="form-control" /><span asp-validation-for="Model" class="text-danger" /></div></div><div class="form-group"><label asp-for="SerialNumber" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="SerialNumber" class="form-control" /><span asp-validation-for="SerialNumber" class="text-danger" /></div></div><div class="form-group"><label asp-for="ConditionID" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="ConditionID" class="form-control" asp-items="ViewBag.ConditionID"><option>---Select Closest Condition---</option></select></div></div><div class="form-group"><label asp-for="EstimateRemainingLife" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="EstimateRemainingLife" class="form-control" /><span asp-validation-for="EstimateRemainingLife" class="text-danger"></span></div></div><div class="form-group"><label asp-for="Recommended" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Recommended" class="form-control" /><span asp-validation-for="Recommended" class="text-danger"></span></div></div><div class="form-group"><label asp-for="InstallDate" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="InstallDate" class="form-control" /><span asp-validation-for="InstallDate" class="text-danger" /></div></div><div class="form-group"><label asp-for="LastServiceDate" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="LastServiceDate" class="form-control" /><span asp-validation-for="LastServiceDate" class="text-danger" /></div></div><div class="form-group"><label asp-for="Notes" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="Notes" class="form-control" /><span asp-validation-for="Notes" class="text-danger" /></div></div><div class="form-group"><label asp-for="ReplacementCost" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="ReplacementCost" class="form-control" /><span asp-validation-for="ReplacementCost" class="text-danger" /></div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-default" /></div></div></div></form><div><a asp-action="Index">Back to List</a></div> @section Scripts { <script src="~/js/WaterBody.js"></script> @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} }
As far as I can tell it just aint working at all. I cant see any reason and cant work out what is going on. Sorry to be a pain. But never used JavaScript before.