c# - 如何将值设置为下拉列表
问题描述
我正在使用 jQuery 在选择第一个下拉列表时设置第二个下拉列表项。在编辑操作时,我想将获取的数据设置为第二个下拉列表。我想设置ViewBag.UserLinkedList
为第二个下拉列表。
View:
<div class="form-row">
<div class="col-lg-7">
@Html.Label("User Type") @Html.DropDownListFor(m => m.LoginUserTypeID(SelectList)ViewBag.LoginUserTypeList, "--- Select User Type ---", new { @class = "form-control" })
</div>
</div>
<div class="form-row">
<div class="col-lg-7">
@Html.Label("Parent User") @Html.DropDownListFor(m => m.UserLinkedID, new SelectList(""), "--- Select ---", new { @class = "form-control" })
</div>
<script>
$(document).ready(function() {
$("#LoginUserTypeID").change(function() {
$.get("/User/GetParentUserList", {
UserTypeID: $("#LoginUserTypeID").val()
}, function(data) {
$("#UserLinkedID").empty();
$.each(data, function(index, row) {
$("#UserLinkedID").append("<option value='" + row.Id + "'>" + row.Name + "</option>")
});
});
})
});
</script>
控制器:
public JsonResult GetParentUserList(int UserTypeID)
{
List<V_Dealer_DealerEmployee> LinkedIDList = new List<V_Dealer_DealerEmployee>();
if (UserTypeID == 1 || UserTypeID == 2)
{
var d = from s in db.VDNSEmployeeMaster
select new
{
Id = s.EmpId,
Name = s.FirstName + " " + s.MiddleName + " " + s.LastName
};
d.ToList();
return Json(d.ToList(), JsonRequestBehavior.AllowGet);
}
else
{
var unionAll = (from word in db.VDNSDealer select new
{
Id = word.m_code,
Name = word.institute_name
}).Concat(from word in db.DealerEngineerMaster select new {
Id = word.EnggId,
Name = word.EngineerName
});
return Json(unionAll.ToList(), JsonRequestBehavior.AllowGet);
}
}
编辑动作
public ActionResult Edit(int id)
{
var user = db.Users.SingleOrDefault(c => c.Id == id);
if (user == null)
return HttpNotFound();
List<LoginUserType> LoginUserTypeList = db.LoginUserType.ToList();
ViewBag.LoginUserTypeList = new SelectList(LoginUserTypeList, "UserTypeId", "UserTypeName");
List<V_Dealer_DealerEmployee> UserLinkedList = db.VDealerDealerEmployee.ToList();
ViewBag.UserLinkedList = new SelectList(UserLinkedList, "Id", "Name");
return View("New", user);
}
解决方案
从操作返回视图后,edit
您需要调用change
. LoginUserTypeID
这是您所需的js
function UpdateUserLinkedIdDropdown(){
var userTypeId = $("#LoginUserTypeID").val();
//This check is for no options selected in LoginUserTypeID Dropdown
if(userTypeId === null || userTypeId === "" || userTypeId === undefined)
userTypeId = 0;
$.get("/User/GetParentUserList", {
UserTypeID: userTypeId
}, function(data) {
$("#UserLinkedID").empty();
$.each(data, function(index, row) {
$("#UserLinkedID").append("<option value='" + row.Id + "'>" + row.Name + "</option>")
});
});
}
$("#LoginUserTypeID").change(function() {
UpdateUserLinkedIdDropdown();
});
// Call it initially when view loaded
// You can do it in this way
UpdateUserLinkedIdDropdown();
// OR This way
$("#LoginUserTypeID").trigger('change');
试试看!我认为这会奏效。如果不评论请
推荐阅读
- g++ - 如何在不依赖任何外部环境变量的情况下使用带有 SCons 的 g++ 构建程序?
- java - 背景鼠标光标 (JavaFX)
- css - Webpack 4 - 缩小从 SCSS 编译的 CSS
- python - 正则表达式操作
- react-native - 如何找出所有应用程序的深层链接路径?
- php - SQLSTATE[HY000]: 一般错误: 21 bad parameter or other API misuse - in pyrocms
- c# - 循环参考/保留周期
- angular - 导出 *(星号)在 Angular/Typescript 中是什么意思
- scala - 在基于 scala 的 spark 结构化流程序中执行基于 python 的模型
- python - 如何在python中保持随机生成的矩阵常数?