Skip to content

Mvc SelectTagHelper name property can't working #63382

@xccu

Description

@xccu

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Our requirement need to model binding input names as format as follow to avoid index gap:
CustomerModel.Contacts[0756264e-600b-47e4-8918-3176d8c57dcb].Name=foo&
CustomerModel.Contacts[0756264e-600b-47e4-8918-3176d8c57dcb].Gender=Male&
&CustomerModel.Contacts.index=0756264e-600b-47e4-8918-3176d8c57dcb

need to bind with name "CustomerModel.Contacts[Xxx].Gender" in our requirement rathor than CustomerModel.Contacts[0].Gender there,
I tried to set SelectTagHelper name="CustomerModel.Contacts[@idx].Name" there but not working.

<select class="form-control valid" data-val="true" data-val-length="The Gender length is 0~10" data-val-length-max="10" data-val-required="The Gender field is required." id="CustomerModel_Contacts_0__Gender" name="CustomerModel.Contacts[0].Gender" aria-describedby="CustomerModel_Contacts_0__Gender-error" aria-invalid="false"> 
  <option>Male</option>
  <option selected="selected">Female</option>
</select>

InputTagHelper set name working there

<input name="CustomerModel.Contacts[0756264e-600b-47e4-8918-3176d8c57dcb].Name"
 class="form-control valid" maxlength="128" type="text" data-val="true" data-val-length="The Name length is 0~128" data-val-length-max="128" data-val-required="The Name field is required." id="CustomerModel_Contacts_1__Name" value="Foo" aria-describedby="CustomerModel_Contacts_1__Name-error" aria-invalid="false">

so set name property in SelectTagHelper will not working when asp-for setted is a bug need to fix? thanks.

the code block as follow

<form>
...
 @for (int index = 0; index < Model.CustomerModel.Contacts.Count; index++)
 {
     string idx = Guid.NewGuid().ToString();
     <tr>
         <td>
             <input type="hidden" name="CustomerModel.Contacts.Index" asp-for="@idx" />   
                  
             <input asp-for="CustomerModel.Contacts[index].Name" name="CustomerModel.Contacts[@idx].Name" class="form-control" maxlength="128" />
             @Html.ValidationMessage($"CustomerModel.Contacts[{@idx}].Name", new { @class = "validation-error" })
         </td>
         <td style="width:140px;">
             @* issue there *@     
             @Html.DropDownList($"CustomerModel.Contacts[{index}].Name", new SelectList(genders), new { @class = "form-control", name = $"CustomerModel.Contacts[{idx}].Gender" })
             <select name="Test Select" asp-for="CustomerModel.Contacts[index].Gender" name="CustomerModel.Contacts[@idx].Gender"  asp-items="@new SelectList(genders)" class="form-control">
             </select>

             @Html.ValidationMessage($"CustomerModel.Contacts[{@idx}].Gender", new { @class = "validation-error" })
         </td>
         <td>
             <button type="button" class="btn btn-link" onclick="removeContactPerson(this);">Delete</button>
         </td>
 </tr>

}
...

Expected Behavior

SelectTagHelper render as:

<select class="form-control valid" data-val="true" data-val-length="The Gender length is 0~10" data-val-length-max="10" data-val-required="The Gender field is required." id="CustomerModel_Contacts_0__Gender" name="CustomerModel.Contacts[0756264e-600b-47e4-8918-3176d8c57dcb].Gender" aria-describedby="CustomerModel_Contacts_0__Gender-error" aria-invalid="false"> 
  <option>Male</option>
  <option selected="selected">Female</option>
</select>

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions