Get Default Value for DropDownListFor from database
-1
I coded a website to collect student information including 3 dropdown lists: Province, District, Ward. When I create a new student, I get the Province, District, Ward data from the database to enter each DropDownListFor
very well, but when I go to the function of editing student information, I don't know how to get the data from the previously saved database to enter each DropDownListFor.
I use ASP.NET Core MVC and Html Helper to do it. I hope you can guide me.
Here is my code
<div class="col-md-2">
<label for="MATINH">Province</label>
@if (ViewBag.TINH != null)
{
@Html.DropDownListFor(m => m.MATINH, ViewBag.TINH as SelectList, "---Select---", new { @class = "form-control" })
}
</div>
<div class="col-md-2">
<label for="MAHUYEN">District</label>
@Html.DropDownListFor(m => m.MAHUYEN, new SelectList(""), "---Select---", new { @class = "form-control" })
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function () {
$("#MATINH").change(function () {
var matinh = $(this).val();
debugger
$.ajax({
type: "post",
url: "/Giaovien/LayHuyen/" + matinh,
contentType: "html",
success: function (response) {
debugger
$("#MAHUYEN").empty();
$("#MAHUYEN").append(response);
}
})
})
})
</script>
</div>
<div class="col-md-2">
<label for="MAXA">Ward</label>
@Html.DropDownListFor(m => m.MAXA, new SelectList(""), "---Seleect---", new { @class = "form-control" })
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script>
$(document).ready(function () {
$("#MAHUYEN").change(function () {
var mahuyen = $(this).val();
debugger
$.ajax({
type: "post",
url: "/Giaovien/LayXa/" + mahuyen,
contentType: "html",
success: function (response) {
debugger
$("#MAXA").empty();
$("#MAXA").append(response);
}
})
})
})
</script>
</div>
</div>