IDI am using the following code to create a drop down list:
@for (var index = 0; index < Model.AdminSummaries.Count(); index++) { <div class="rep_tr0"> <div class="rep_td0"> @Html.DropDownListFor(x => Model.AdminSummaries[index].Status, AdminStatusReference.GetAdminStatusOptions()) </div>
The code creates the following:
<select id="AdminSummaries_2__Status" name="AdminSummaries[2].Status"> <option value="1">Released</option> <option value="2">Review</option> <option value="3">New</option> </select> <select id="AdminSummaries_3__Status" name="AdminSummaries[3].Status"> <option value="1">Released</option> <option value="2">Review</option> <option value="3">New</option> </select>
Is there any way that I could change it so that it creates a ID of "Status_2", "Status_3" etc.
In order to set html attributes inside @Html. DropDownListFor , you have to set 4th parameter of @Html. DropDownListFor with new keyword which will create a object for htmlattributes .
Like @Anar said in the comments;
Actually, you can change the name attribute the same way as id, but instead of "name", use "Name". Surprisingly it works.
@Html.DropDownListFor(x => Model.AdminSummaries[index].Status, AdminStatusReference.GetAdminStatusOptions(), new { id = string.Format("Status_{0}",index ), Name = "GiveName" });
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With