Can I change the html name generated by Html.DropDownListFor?

前端 未结 9 1611
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 03:04

IDI am using the following code to create a drop down list:

 @for (var index = 0; index < Model.AdminSummaries.Count(); index++) 
            { 
            &         


        
相关标签:
9条回答
  • 2021-02-05 03:47

    Sometimes the HTML helpers don't HELP. DropDownListFor coding can get complicated real fast and in the end it's just rendering HTML so sometimes it's better to go old school

    <select name="myname" class="dropdownlist" id="myid">
        @foreach (SelectListItem item in Model.SomeSelectList) {
            if (item.Value ==  Model.theValue) {
                <option value="@(item.Value)" selected="selected">@item.Text</option>
            } else {
                <option value="@(item.Value)">@item.Text</option>
            }
        }
    </select>
    
    0 讨论(0)
  • 2021-02-05 03:47

    You can set/change the ID just like any HTML attribute (but not name I've found).

    @Html.DropDownListFor(x => Model.AdminSummaries[index].Status, AdminStatusReference.GetAdminStatusOptions(), new { id = string.Format("Status_{0}",index ) });
    
    0 讨论(0)
  • 2021-02-05 03:51

    Thanks .. it work...adding .Name change the name of the html password attribute

          @Html.PasswordFor(Function(model) model.password, New With {.Autocomplete = "off", .Name = "oldpassword", .id = "oldpassword", .value = Model.password, .readonly = True})
    
    0 讨论(0)
提交回复
热议问题