Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

ModelExpression equivalent of m => m: Better support asp-for="@Model" #3978

Closed
alex-novikov-1990 opened this issue Jan 23, 2016 · 7 comments
Closed
Assignees
Milestone

Comments

@alex-novikov-1990
Copy link

How to use label tag helper in the following scenario in a display template? asp-for="@Model" leads to invalid label text ("Model").

@Html.LabelFor(m => m, new { @class = "control-label col-md-2" })
@NTaylorMullen
Copy link
Contributor

<label asp-for="@Model" class="control-label col-md-2"></label>

@alex-novikov-1990
Copy link
Author

In a display template (e.g. @Html.DisplayFor(m => m.UserName)):

  • <label asp-for="@Model"></label> results in <label for="UserName">Model</label>
  • @Html.LabelFor(m => m) results in <label for="UserName">UserName</label>

@dougbu
Copy link
Member

dougbu commented Jan 24, 2016

I'm able to reproduce this as well. Haven't debugged what is going South but it does look strange, especially given the for attribute has the correct value.

@dougbu
Copy link
Member

dougbu commented Jan 24, 2016

FYI problem relates to how the ModelMetadata for the property is found. In the @Html.LabelFor(m => m) case, PropertyName is "UserName". But in the <label asp-for="@Model"></label> case, PropertyName is "Model". Looking a bit deeper...

@dougbu
Copy link
Member

dougbu commented Jan 24, 2016

🆗 the issue here is that ExpressionMetadataProvider.FromLambdaExpression() correctly handles m => m and other identity expressions (ExpressionType.Parameter) but does not understand that m => {generated class}.Model needs special handling. That is, @Html.LabelFor(m => Model) hits exactly the same naming issue.

Unfortunately tag helpers do not support lambda identity expressions and always hit this issue if the @Model "fallback" is used. We probably didn't see this before because (a) the ModelMetadata.PropertyName property isn't used extensively in HTML generation and (b) @Model.UserName, @Model[i].UserName, and other lambda RHS that start with @Model work fine.

One caveat on (a): strings generated for client-side validation rules can be messed up when PropertyName is incorrect e.g. data-val-required="The Model field is required.".

@dougbu
Copy link
Member

dougbu commented Jan 24, 2016

And, no, I don't know of an RC1 workaround other than to use HTML helpers for this case.

@danroth27 danroth27 added this to the 6.0.0-rc2 milestone Jan 26, 2016
@Eilon Eilon modified the milestones: 1.0.0-rc2, 1.0.0 Mar 13, 2016
dougbu added a commit that referenced this issue Apr 14, 2016
…Provider`

- #3978
- better-aligns `ExpressionMetadataProvider` with `ExpressionHelper`

nit: mock less in `RazorPageCreateModelExpressionTest`
@Eilon Eilon modified the milestones: 1.0.0-rc2, 1.0.0 Apr 15, 2016
dougbu added a commit that referenced this issue Apr 16, 2016
…Provider`

- #3978
- better-aligns `ExpressionMetadataProvider` with `ExpressionHelper`

nit: mock less in `RazorPageCreateModelExpressionTest`
dougbu added a commit that referenced this issue Apr 16, 2016
…Provider`

- #3978
- better-aligns `ExpressionMetadataProvider` with `ExpressionHelper`

nit: mock less in `RazorPageCreateModelExpressionTest`
@dougbu
Copy link
Member

dougbu commented Apr 16, 2016

release: 7a1ac03
also merged into dev

@dougbu dougbu closed this as completed Apr 16, 2016
@dougbu dougbu changed the title ModelExpression equivalent of m => m? ModelExpression equivalent of m => m: Better support asp-for="@Model" Apr 16, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants