I have here 2 viewbag in my controller
ViewBag.MyCategory = new SelectList(db.RecyclableType, "id", "type");
ViewBag.iddd = new SelectList(db.RecyclableType, "id", "type");
and a View
@Html.DropDownList("iddd", (IEnumerable<SelectListItem>)ViewBag.iddd, "SELECT ITEM TYPE", new { @class = "form-control" })
@Html.DropDownList("MyCategory", (IEnumerable<SelectListItem>)ViewBag.MyCategory, "SELECT ITEM TYPE", new { @class = "form-control"})
i tried to create a function like below but nothing is working
document.getElementById("iddd") = document.getElementById("MyCategory");
You can do it using jQuery
iddd
dropdown get its value
MyCategory
dropdown and set the attribute to selected
$("#iddd").change(function () {
var value = $("#iddd").val();
$("#MyCategory").val(value).attr("selected", "selected");
});
Note: Add jQuery script file in head section
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.