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

HTML and tag helpers will NRE if given a Linq expression w/ a static property #4856

Closed
dougbu opened this issue Jun 11, 2016 · 2 comments
Closed
Assignees
Milestone

Comments

@dougbu
Copy link
Member

dougbu commented Jun 11, 2016

MVC generally does not support static properties e.g. ModelMetadata.Properties excludes static properties. However the failure mode is poor when a static property is used in a Linq expression. ExpressionMetadataProvider throws an NRE and does not give the user information about their mistake.

Repro
  1. Add a public static string StaticString { get; set; } property to a model type.
  2. Add a Linq expression using the static property in a view e.g. @Html.EditorFor(m => TopLevelClass.StaticString)
  3. Start the site
  4. Navigate to page using the view
Expected

A coherent error stating the StaticString property does not exist or the expression is not supported.

Actual

staticstring

Regression?

No; MVC 5 has the same issue.

Potential fix
  1. null-check memberExpression.Expression before dereferencing it.
  2. Either
    1. Add static properties to the cases ExpressionMetadataProvider supports. This would be similar to how this class supports Linq expressions containing fields though MVC's field support is generally very limited.
    2. Include use of a static property in the cases where ExpressionMetadataProvider throws an InvalidOperationException. Probably easiest to add the word "instance" once or twice in Resources.TemplateHelpers_TemplateLimitations and use the existing legalExpression variable.
@dougbu
Copy link
Member Author

dougbu commented Jun 11, 2016

My thanks to @mnltejaswini who first discovered this issue when reviewing the code.

@Eilon Eilon added this to the Backlog milestone Jun 13, 2016
@rynowak
Copy link
Member

rynowak commented Jul 17, 2017

This was fixed by a23307e

@rynowak rynowak closed this as completed Jul 17, 2017
@rynowak rynowak modified the milestones: 2.0.0, Backlog Jul 17, 2017
@rynowak rynowak self-assigned this Jul 17, 2017
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

4 participants