问题
I was trying to fill a DropDownList based on the value from another DropDownList in MVC .NET Core 2.2.
<div class="form-group row">
<label class="col-sm-2 col-form-label">Type :</label>
<div class="col-sm-10">
<select asp-for="SelectedType" asp-items="@(new SelectList(Model.TypeList, "TypeName", "TypeDescription"))" class="custom-select custom-select-sm">
</select>
</div>
</div>
public class Type
{
public string TypeName { get; set; }
public string TypeDescription { get; set; }
}
I'm not familiar with JavaScript or Ajax and I can't figure the example on Google out.
So basically if you select in DropDownList "Type" for example "A", the DropDownList needs to be filled with a list of strings based on "A", when you select "B" the DropDownList needs to be filled with a list of strings based on "B".
Can someone provide me a very easy example?
Something like this?
$(document).ready(function()
{
$("#ddlEmployee").on("change", function()
{
$.ajax(
{
url: '/Home/GetTypesBasedOnValueFromOtherDropDownList' + $(this).attr("value"),
type: 'GET',
data: "",
contentType: 'application/json; charset=utf-8',
success: function(data)
{
$("#???").html(data);
},
error: function()
{
alert("error");
}
});
});
});
< /script>
回答1:
<asp:DropDownList ID="DropDownBox2" runat="server" Width="168px">
<asp:ListItem Value="%" Text="Option 1"></asp:ListItem>
<asp:ListItem Value="Option 2"></asp:ListItem>
<asp:ListItem Value="Option 3"></asp:ListItem>
<asp:ListItem Value="Option 4"></asp:ListItem>
<asp:ListItem Value="Option 5"></asp:ListItem>
<asp:ListItem Value="Option 6"></asp:ListItem>
in page_load
if (DropDownList1.Text == "Condition 1")
{
DropDownBox2.Items[3].Enabled = false;
DropDownBox2.Items[4].Enabled = false;
DropDownBox2.Items[5].Enabled = false;
}
This would make options 0,1,2 visible only in the second drop down box.
If you need it to be instant and without page_load see this working example of a jscript solution: https://jsfiddle.net/k148pk76/1/
回答2:
You can pass selected value of the first select
to server side , query the database and return result back to client side , in success callback function of ajax to fill the second select
. Code below is for your reference :
$("#SelectedType").on("change", function () {
$.ajax({
type: 'GET',
url: "/home/GetTypesBasedOnValueFromOtherDropDownList",
contentType: 'application/json',
data: { Type: $("#SelectedType").val() },
success: function (data) {
var s = '<option value="-1">Please Select a Course</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].courseCode + '">' + data[i].courseName + '</option>';
}
$("#secondSelect").html(s);
}
});
})
Server side :
public IActionResult GetTypesBasedOnValueFromOtherDropDownList(string Type)
{
//you can query database instead
List<AdmInstCourses> admInstCourses = new List<AdmInstCourses>();
admInstCourses.Add(new AdmInstCourses() { CourseCode = 1, CourseName = "name1", Units = "3.2" });
admInstCourses.Add(new AdmInstCourses() { CourseCode = 2, CourseName = "name2", Units = "3.3" });
return new JsonResult(admInstCourses);
}
来源:https://stackoverflow.com/questions/58735515/fill-a-dropdownlist-based-on-the-selected-value-from-another-dropdownlist