iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 27
0
Modern Web

我的30天MVC從零到不知道多少學習筆記系列 第 27

從零開始的MVC開發-使用HtmlHelper產生SelectList

  • 分享至 

  • twitterImage
  •  

HtmlHelper可以幫助我們快速產生html!
今天來寫寫DropDownList和DropDownListFor的用法~
這兩者都是用來產用標籤以及選項標籤的方法
簡單來說就是幫忙產生個下拉式選單以及其值啦~

兩者的差異在於DropDownListFor可以帶入Model屬性!

以下使用簡單範例做做DropDownList
首先必須將下拉選單的值從controller使用viewBag或viewData方式帶過去
這段寫在Action內~

var peopleList=new People[]{
    new People{Name="TOM",id=1},
    new People{Name="Tony",id=2},
    new People{Name="Bob",id=3},
    new People{Name="David",id=4}
};
SelectList list=new SelectList(peopleList,"id","Name");
ViewBag.peopleList=list;

View內加上這句
直接寫上viewBag的參數就可!
@Html.DropDownList("peopleList")
如果要改下拉式選單的name的話,可以這樣寫
@Html.DropDownList("peopleID",(SelectList)ViewBag.peopleList)


上一篇
從零開始的MVC開發-顯示alert提示框
下一篇
從零開始的MVC開發-HtmlHelper.ActionLink以及button
系列文
我的30天MVC從零到不知道多少學習筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言