Samantha J T Star
Samantha J T Star

Reputation: 32808

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

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.

Upvotes: 36

Views: 36283

Answers (9)

hmz
hmz

Reputation: 1017

As @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" });

Upvotes: 85

user11441779
user11441779

Reputation:

You need to change From DropDownListFor To DropDownList. Than you can change name easily.

@Html.DropDownList("YourName", SelectList, new { @id = "YourId", @class = "YourClass" })

Hope It will work.

Upvotes: 3

mtholen
mtholen

Reputation: 1663

For those wondering why this doesn't work, the whole thing is case sensitive and need an @ in front of it...

so, this works:

new { @id = "myID", @Name = "myName", @Value = "myValue", @class = "form-control", @onchange = "javascript:DoSomething(this.value);" }

And this doesn't (mind the lowercase 'n' in @name)

new { @id = "myID", @name = "myName", @Value = "myValue", @class = "form-control", @onchange = "javascript:DoSomething(this.value);" }

Upvotes: 13

Jitendra Bansiwal
Jitendra Bansiwal

Reputation: 123

Just use "Name" instead of "name" and it works.

@Html.DropDownList("ClassID", null, "Select Class", htmlAttributes: new { id = _id, Name =_id, @class = "form-control" })

Upvotes: 4

RitchieD
RitchieD

Reputation: 1861

My solution to this issue is to replace the id and name after DropDownListFor() with a html helper extension method.

        public static MvcHtmlString ReplaceIdAndName(this MvcHtmlString htmlSource, string name)
    {
        MvcHtmlString result;
        if (name == null)
            result = htmlSource;
        else
        {
            string id = name.Replace("[", "_").Replace("]", "_").Replace(".", "_");  // HTML ids cannot have []. so MVC convention replaces with _
            XDocument html = XDocument.Parse(htmlSource.ToHtmlString());
            html.Elements().Select(e => e.Attribute("id")).FirstOrDefault().SetValue(id);
            html.Elements().Select(e => e.Attribute("name")).FirstOrDefault().SetValue(name);
            result = MvcHtmlString.Create(html.ToString());
        }
        return result;
    }


DropDownListFor(...).ReplaceIdAndName("myclass[1].myprop");

Upvotes: 0

Yugam
Yugam

Reputation: 11

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})

Upvotes: 1

nuander
nuander

Reputation: 1403

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>

Upvotes: 13

TZHX
TZHX

Reputation: 5377

ID can be changed as described by @Arbiter. However, if you want to change the name, then I think you need to use @Html.DropDownList rather than @Html.DropDownListFor. This loses you any benefits of having the strong typing for the property element, but as just changing IDs means you can't access the values in a meaningful way if looking at the Request.Form response, this may be an acceptable workaround for you.

Upvotes: 2

Arbiter
Arbiter

Reputation: 1024

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 ) });

Upvotes: 6

Related Questions