Skip to content
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

AddModelError does not work for expressions that ends with indexer. #58271

Open
1 task done
matejmn opened this issue Oct 7, 2024 · 0 comments
Open
1 task done

AddModelError does not work for expressions that ends with indexer. #58271

matejmn opened this issue Oct 7, 2024 · 0 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@matejmn
Copy link

matejmn commented Oct 7, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

AddModelError extension method does not work for expressions that ends with indexer (e.g. m => m.Numbers[0]).

The problem is that every expression that do not directly returns object is wrapped by Convert expression and IsConversionToObject method returns true only if NodeType equals to ExpressionType.MemberAccess.
I think it should return true also for ExpressionType.Call or ExpressionType.ArrayIndex.

If I run this code in C# Interactive concole in Visual Studio:

#r "c:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\8.0.7\Microsoft.AspNetCore.Mvc.ViewFeatures.dll"
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
class Model
{
    public List<int> Numbers { get; set; }
}
var state = new ModelStateDictionary();
state.AddModelError<Model>(m => m.Numbers[0], "Error");
state.Keys.First()

it returns empty string:

""

Expected Behavior

It should return:

"Numbers[0]"

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

1 participant