AddModelError does not work for expressions that ends with indexer.
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
[AddModelError](https://github.com/dotnet/aspnetcore/blob/6a40a23cd9242fc7ea01a9ae4f58886068f7257c/src/Mvc/Mvc.ViewFeatures/src/ModelStateDictionaryExtensions.cs#L25) 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](https://github.com/dotnet/aspnetcore/blob/6a40a23cd9242fc7ea01a9ae4f58886068f7257c/src/Mvc/Mvc.ViewFeatures/src/ModelStateDictionaryExtensions.cs#L164C25-L164C45) 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 Numbers { get; set; }
}
var state = new ModelStateDictionary();
state.AddModelError(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_
Contributor guide
Assessment
This issue has not been assessed yet.