Html.Listbox的用法(執行個體)

來源:互聯網
上載者:User

標籤:des   c   style   class   blog   code   

homecontroller控制器中的index動作代碼如下:

        public actionresult index()
        {

            list<selectlistitem> list = new list<selectlistitem>() 
            { 
                new selectlistitem(){text="張三",value="001"},
                new selectlistitem(){text="李四",value="002"},
                new selectlistitem(){text="王五",value="003"},
                new selectlistitem(){text="趙六",value="004"},
                new selectlistitem(){text="田七",value="005",selected=true}
            };

            viewdata["namelist"] = list;

            return view();
        }

對應的view,將資料繫結到view的html.dropdownlist和html.listbox上

         <%
                selectlist list
=viewdata["namelist"] as selectlist;    
         
%>
         <!--注意使用dropdownlist或listbox指定第一個參數name,
         會當成鍵自動到viewdata的集合viewdatadirectory中去搜尋對應的值,
         搜尋不到則報異常.所以這裡name參數必須和viewdata所存放資料的索引值一樣
-->
        <%=html.dropdownlist("namelist", list)%>
        <!--直接指定名稱也可以完成資料繫結,因為前面說了會到viewdatadirectory去搜尋-->
         <%=html.listbox("namelist")%>
         <!--由於指定的同一個namelist作為html控制項的id和name所以我們要修改其id,因為在html中id通常是唯一的-->
         <%=html.listbox("namelist", list, new { id="mynamelist"})%>

產生的html代碼如下:

 

 

原文轉載自:http://www.cnblogs.com/jhxk/articles/2630967.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.