Skip to content

Html.NameFor does not correctly generate name when field has two underscores #18913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
stefanodelpero opened this issue Feb 9, 2020 · 9 comments
Labels
affected-few This issue impacts only small number of customers area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views severity-major This label is used by an internal tool
Milestone

Comments

@stefanodelpero
Copy link

Fields with double underscore in name (I've to use a legacy system fields naming) disappear in Request.Form.

So, if I have an object like this:

public class Company
{
  public string Field__Name { get; set; } // 2 or more underscore between Field and Name
}

And a cshtml page with:

<input asp-for="Field__Name" />

The POST form action send the form value to the server but ASP.NET Request.Form didn't have the Field__Name field. Also the field is null in the OnPost parameter object.

Further technical details

  • ASP.NET Core 3.1
@Tratcher
Copy link
Member

Tratcher commented Feb 9, 2020

Can you share a Fiddler trace showing the raw form post?

@stefanodelpero
Copy link
Author

With this class:

public class Company2
{
  public string Field__Name { get; set; }
  public string OtherField { get; set; }
}

and these values in form:
Field__Name = 111
OtherField = 222

This is the raw form post:

POST https://localhost:44310/ HTTP/1.1
Host: localhost:44310
Connection: keep-alive
Content-Length: 262
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4041.0 Safari/537.36 Edg/81.0.410.1
Content-Type: application/x-www-form-urlencoded
Origin: https://localhost:44310
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: same-origin
Sec-Fetch-Dest: empty
Referer: https://localhost:44310/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,it;q=0.8
Cookie: .AspNetCore.Antiforgery.VFg60T-jNbs=CfDJ8IGu5-fGa-hMvjpjhbbwAT2wBeUpFLjtv2Zl7tNxsdplZxqaBJMLl7LN5gaZpezeC0d3CfSu7m2vFYeecBzjtCSNPMDCWyY-kKt2zPbggCon8os9n9eVFEuwWTuUHPIwSmpnAEZK8IxIGYkC_U9jxlM; .AspNetCore.Identity.Application=CfDJ8IGu5-fGa-hMvjpjhbbwAT28zMtnMGXxqIiBurU1FfRmocqgJ7_IrrebLKfuimpwt7PLsNcyN7MjIzD9ld9SFbolBX6lCelBeJMHSO3Tcr1RCpr-YDUgDcjVvRytSbEqsU4O2_hSlIU5fttDbN_mo0gSqZzenxU2OAI8GS3WEF-vJcJZT5XRoxlsD1yb9NiXLdQTC9UWX3LDzqn8eFiQ8S00kOxncR1ul0WKaPYvzswFjCi9k8G_fQQ5Clcjf2JflVJhCbt2FSyw36GvhAumda-2i0T51VQTLStTBR3HFEB8CmqXnZXOVvqnriRZWl_rpf5ioBfTiVbGsRU6Fs_dV1YYXD0r8FYtuJOP6K05xeznlxS1Yr1Y1Mg02ZFNEHATYajW2ohmqmdRtPqaWqOf44TmKhnntIB6yqOCKArU3-J63AKQxsfegKCwx2-3zKgBf995ghizKeByyFACtIPNKS6OZd6RH8GqZC2lekDPhaIiUnYSPXDY8LMumiJx6vKng6VfOdHv2_WpDbntkofMBJyzJKyywpX9SqRl2IT08DU1mBZk_Z98_PRPzeSuHWCOXwRMzjWdCN1R_LsOdHOmbS0CgROgYguQih2IPG5LS5POTotcdk4sOcpqzI3A7TvbyvXm5SyMlLuBclKYZpVWWuCM_6mwDsHsvrfGi2LReMZvHjp9IvQ7e3kGME4XW6yr5A

Company2=111&Company2.OtherField=222&__RequestVerificationToken=CfDJ8IGu5-fGa-hMvjpjhbbwAT3u0fwP-rO_dLGaAajzfdlj_bGVAlSfQwFxlqeTQ99vdgvltQEZ6eYM98RAeZer8iJJHZ2DsqwDxl4FGMb_17ftklQTSDFWudJxH8TMkYWrwQ_zMiuKaCG09XreKVF0HctHzxGMSS5N71kK-1r9wYb9xCYyvP1ZCTNC5jQwKey5lg

@stefanodelpero
Copy link
Author

This is the HTML generated by ASP.NET Core:

<div class="row">
    <div class="col">
        <div class="form-group">
            <label for="Company2">Field__Name</label>
            <input class="form-control" autofocus type="text" id="Company2" name="Company2" value="" />
            <span class="text-danger field-validation-valid" data-valmsg-for="Company2" data-valmsg-replace="true"></span>
        </div>

        <div class="form-group">
            <label for="Company2_OtherField">OtherField</label>
            <input class="form-control" type="text" id="Company2_OtherField" name="Company2.OtherField" value="" />
            <span class="text-danger field-validation-valid" data-valmsg-for="Company2.OtherField" data-valmsg-replace="true"></span>
        </div>
    </div>
</div>

@Tratcher
Copy link
Member

Tratcher commented Feb 9, 2020

Values in the form POST:
Company2=111
Company2.OtherField=222
__RequestVerificationToken=CfDJ8IGu5...

So this isn't a problem with parsing the form data on the server, it's a problem with generating the correct HTML to submit the form?

Moving back to MVC.

@Tratcher Tratcher added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates and removed area-servers labels Feb 9, 2020
@stefanodelpero
Copy link
Author

Yes.
Company2.Field__Name become

<input type="text" id="Company2" name="Company2" value="" />

instead of

<input type="text" id="Company2_Field__Name" name="Company2.Field__Name" value="" />

@javiercn
Copy link
Member

@epic2001 thanks for contacting us.

I'm not sure if this is a bug or it is a consequence of the current design. We will look into this and get back to you.

@javiercn javiercn added this to the 5.0.0-preview2 milestone Feb 10, 2020
@stefanodelpero
Copy link
Author

Ok! Thank you!

@pranavkm
Copy link
Contributor

pranavkm commented May 1, 2020

@stefanodelpero looks like asp-for has some specific code paths for properties that contain __. The background seems to be that __ appear frequently in compiler generated types which appear commonly in lambda expressions and caused MVC to generate incorrect names. aspnet/Mvc#2890, aspnet/Mvc@a045324.

A fix for this could attempt to inspect the declaring type has a compiler generated attribute here:

. That said, given this is the first we've heard of this bug, and we don't have enough test coverage to verify changing this doesn't affect other esoteric scenarios, I'm hesitant to address this right away. We'll park this in the backlog and reconsider this if we get further feedback.

In the meanwhile, there are a couple of options to workaround this:

a) Use a different property name
b) Explicitly specify the field name:
<input type="text" id="Company2_Field__Name" name="Company2.Field__Name" value="" /> (from your sample)

@pranavkm pranavkm removed their assignment May 1, 2020
@pranavkm pranavkm added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels May 1, 2020
@pranavkm pranavkm modified the milestones: 5.0.0-preview4, Backlog May 1, 2020
@pranavkm pranavkm changed the title Form fields with double underscore __ are not in Request.Form Html.NameFor does not correctly generate name when field has two underscores May 1, 2020
@captainsafia captainsafia added affected-few This issue impacts only small number of customers severity-major This label is used by an internal tool labels Mar 15, 2021 — with ASP.NET Core Issue Ranking
@javiercn javiercn added the feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views label Apr 18, 2021
@mkArtakMSFT
Copy link
Contributor

Hi. Thanks for contacting us.
We're closing this issue as there was not much community interest in this ask for quite a while now.
You can learn more about our triage process and how we handle issues by reading our Triage Process writeup.

@mkArtakMSFT mkArtakMSFT closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views severity-major This label is used by an internal tool
Projects
None yet
Development

No branches or pull requests

6 participants