MVC實現RadioButtonList

來源:互聯網
上載者:User

在MVC3裡只有RadioButton,沒有RadioButtonList,但項目中又要用到,怎麼辦?

答案:自己看著辦。

實現後的功能:有一個檔案保密等級,如絕密、機密、一般等,需要在頁面中顯示,並且可以後台控制哪個被選中。

實現步驟如下:

1,將保密等級及是否選中,載入到一個List中。

View Code

                List<KeyValuePair<string, bool>> secrecyList = new List<KeyValuePair<string, bool>>();                var secrecyLevelList = baseTypeDetailRepository.GetBaseTypeDetailList(BaseType.SecrecyLevel);                foreach (var item in secrecyLevelList)                {                    if (auth.SecrecyLevel == item.Caption)                    {                        secrecyList.Add(new KeyValuePair<string, bool>(item.Caption, true));                    }                    else                    {                        secrecyList.Add(new KeyValuePair<string, bool>(item.Caption, false));                    }                }

2,在頁面中遍曆List,判斷是否選中,並輸入為RadioButton

View Code

                    @if (ViewBag.SecrecyList != null)                    {                        foreach (KeyValuePair<string, bool> item in ViewBag.SecrecyList)                        {                            if (item.Value)                            {                        @Html.RadioButton("SecrecyLevel", item.Key, new { @id = item.Key, @checked = true }) <span> @item.Key </span>                            }                            else                            {                        @Html.RadioButton("SecrecyLevel", item.Key, new { @id = item.Key }) <span> @item.Key </span>                                                            }                        }                    }

3,後台可接收選中檔案保密等級。

var secrecyLevel = collection["SecrecyLevel"];

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.