简体   繁体   中英

Changing the control ID of a dropdownlist in mvc

I have two dropdownlist which grabs data from a ViewData. The viewData holds a SelectedListItem, which is a set of times (1pm,2pm,3pm..) the time doesn't really have anything to do with the model (as I convert it to UTC) so that is why I created it as a viewbag. I have it used in two spots in the view, but the code is producing the same IDs

@Html.DropDownList("times",null,new {@class="ddl"})

<select class="ddl" id="times" name="times"><option value="0">1PM</option>

I think it's relatively easy to change the ID, I just can't find the exact spot within the Html.DropDownList

Change the value of the first parameter, that is what sets the ID

@Html.DropDownList("times1",null,new {@class="ddl"})

@Html.DropDownList("times2",null,new {@class="ddl"})

此外,由于它会自动从ViewBag中选择名称,因此对于需要多次显示同一列表的情况,我们可能必须定义多个ViewBag属性,这些属性均引用相同的后端列表数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM