Asked by:
javascript function is redirecting to wrong view drop down

Question
-
User-1355965324 posted
I am using a general function FillLocationsDropdown(), to select locations from the dropdown. The function is being called from the two views
- Holiday.cshtml 2. Attendance.cshtml. If I select the drop down of Location from Attendance.cshtml , automatically will redirect to Holiday.cshtml. The screen automatically changed from Attendance to holiday html. How can i fix this issue. this is my script given below. But If I select location from attendance it will work fine. But If I use the attendance screen once after the use of Holiday then when I click the dropdown in attendance, suddenly the view automatically changed in holiday . how can it be fixed please help
holiday html
<div class="form-group"> <label class="control-label control-label-left col-sm-3 text-danger" for="field1">Depot</label> <div class="controls col-sm-4"> <select id="dropdownDepot" class="form-control" asp-for="DepotNo" asp-items="@ViewBag.UserDepots" onchange="FillLocationsDropdown()"></select> <span asp-validation-for="DepotNo" class="text-danger"></span> </div> <div class="controls col-sm-5"> <div class="form-group"> <label class="control-label control-label-left col-sm-5 text-danger" for="field1">Department</label> <div class="controls col-sm-7"> <select id="dropdownDepartment" class="form-control" asp-for="DepartmentID" asp-items="@ViewBag.UserDepartments" onchange="FillEmployeeDropdown()"></select> <span asp-validation-for="DepartmentID" class="text-danger"></span> </div> </div> </div> </div> </div>
Attendance html
<div class="row col-sm-8"> <div class="form-group"> <label class="control-label control-label-left col-sm-2" for="field2"> Depot</label> <div class="controls col-sm-10" style="padding-right: 0px !important;"> <select id="dropdownDepot" class="form-control" multiple asp-for="Depot" onchange=" FillLocationsDropdown()" asp-items="@ViewBag.UserDepots" data-role="select"></select> </div> </div> </div> <div class="col-sm-8 row"> <div class="form-group"> <label class="control-label control-label-left col-sm-2" for="field2"> Department</label> <div class="controls col-sm-10" style="padding-right: 0px !important;"> <select id="dropdownDepartment" multiple class="form-control" asp-for="Department" onchange="FillEmployeeDropdown()" asp-items="@ViewBag.UserDepartments" data-role="select"></select> </div> </div> </div>
javascript function from ddl.js
function FillLocationsDropdown() { var depot = $('#dropdownDepot option:selected'); var depots = ""; $(depot).each(function (index, brand) { depots = depots + $(this).val() + ","; }); if (depots == "") { $('#dropdownEmployee').empty(); $('#dropdownEmployee').multiselect('reload'); $('#dropdownDepartment').empty(); $('#dropdownEmployee').multiselect('reload'); } else { $.ajax({ type: "GET", url: "/User/GetUserDepotLocations?depots=" + depots, contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { // var data = JSON.parse(response); $('#dropdownDepartment').empty(); var Names = document.getElementById('dropdownDepartment'); Names.length = 0; if (data.length > 0) { $.each(data, function (i) { opt = document.createElement('option'); Names.options.add(opt); opt.text = data[i].text; opt.value = data[i].value; if (data[i].selected) { opt.setAttribute('selected', 'selected'); } }); $('#dropdownDepartment').multiselect('reload'); } else { $('#dropdownDepartment').empty(); $('#dropdownDepartment').multiselect('reload'); $('#dropdownEmployee').empty(); $('#dropdownEmployee').multiselect('reload'); } }, failure: function (response) { console.log(response.responseText); }, error: function (response) { console.log(response.responseText); } }); } }
Monday, August 10, 2020 10:31 PM
All replies
-
User-17257777 posted
Hi polachan,
If I select the drop down of Location from Attendance.cshtml , automatically will redirect to Holiday.cshtml. The screen automatically changed from Attendance to holiday html. How can i fix this issue.I am a little confused according to your code and description, is there any relationship between the two views, are they two partial views? Also, there seems no redirection in your code, I can't figure out when it will redirect.
There is #dropdownEmployee in your scripts, while I can't find any element with this id in your html codes. Maybe you can provide us more information, including the whole page, the related model class and controller action, it may help us reproduce your problem.
Best Regards,
Jiadong Meng
Tuesday, August 11, 2020 5:37 AM