下拉選擇框 設(shè)置內(nèi)容 獲取選中項(xiàng) 設(shè)置選中項(xiàng)

設(shè)置內(nèi)容
Html.DropDownList創(chuàng)建下拉框,下拉框名稱為"useState",集合為ViewBag.UseStates,值是DicKey,顯示是DicValue

<div>
    使用狀態(tài)
    @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()">獲取選擇項(xiàng)</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; }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評(píng)論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,838評(píng)論 4 61
  • 在這里 關(guān)于悲歡離合 在這里 關(guān)于生老病死 在這里 關(guān)于疾病與痛苦 死亡與重生 一幕幕悲劇重演 一行行淚水不斷 我...
    馨之所翔閱讀 180評(píng)論 0 0
  • 在數(shù)學(xué)上,數(shù)軸上原點(diǎn)為O點(diǎn),坐標(biāo)軸的交點(diǎn)就是坐標(biāo)系統(tǒng)的原點(diǎn)。 地球的形狀近似于一個(gè)橢球,但它的表面很不規(guī)則,有高有...
    白語金言閱讀 2,175評(píng)論 1 3

友情鏈接更多精彩內(nèi)容