下拉选择框 设置内容 获取选中项 设置选中项
设置内容
Html.DropDownList创建下拉框,下拉框名称为"useState",集合为ViewBag.UseStates,值是DicKey,显示是DicValue
<div>
使用状态
@Html.DropDownList("useState", new SelectList(ViewBag.UseStates, "DicKey", "DicValue"))
@{
var UseState = ViewBag.UseState.ToString();
if (!string.IsNullOrEmpty(UseState))
{
if (UseState.Contains("1"))
{
<script>$("#useState").find("option:selected").val(1);</script>
}
else if (UseState.Contains("2"))
{
<script>$("#useState").find("option:selected").val(2);</script>
}
else if (UseState.Contains("3"))
{
<script>$("#useState").find("option:selected").val(3);</script>
}
}
}
<button onclick="GetSelectOne()">获取选择项</button>
</div>
<script>
function GetSelectOne() {
var UseState = $("#useState").find("option:selected").val();
layer.alert(UseState);
}
</script>
public ActionResult DropDownList()
{
List<DictionaryModel> dic = new List<DictionaryModel>();
dic.Add(new DictionaryModel() { PKID = 1, DicKey = "1Using", DicValue = "使用中" });
dic.Add(new DictionaryModel() { PKID = 2, DicKey = "2", DicValue = "未投入使用" });
dic.Add(new DictionaryModel() { PKID = 3, DicKey = "3", DicValue = "已废弃" });
ViewBag.UseStates = dic;
ViewBag.UseState = 1;
return View();
}
DictionaryModel
public class DictionaryModel
{
public int PKID { get; set; }
public string DicKey { get; set; }
public string DicValue { get; set; }
}
学习技术最好的文档就是【官方文档】,没有之一。
还有学习资料【Microsoft Learn】、【CSharp Learn】、【My Note】。
如果,你认为阅读这篇博客让你有些收获,不妨点击一下右下角的【推荐】按钮。
如果,你希望更容易地发现我的新博客,不妨点击一下【关注】。