Aspnetcore: HtmlHelper.Editor method doesn't correctly render properties of an object inside a collection

Created on 31 Oct 2019  路  3Comments  路  Source: dotnet/aspnetcore

Bug

HtmlHelper Editor method erroneously render a property of an object in a collection. EditorFor method works correctly.

To Reproduce

Steps to reproduce the behavior:

  1. ASP.NET Core 3.0.0, Windows 10, Visual Studio 16.3.7
  2. Create a new Project
  3. Create class MyItemViewModel:
public class MyItemModel 
{
   public string Name { get; set; }
   public int Age { get; set; }
}
  1. Create class MyViewModel
public class MyViewModel
{
  public List<MyItemModel> Items { get; set; } = new List<MyItemModel>();
}
  1. Create an action that populates the view model and returns a view for the model
public IActionResult Index()
{
  var model = new MyViewModel();
  model.Items.Add(new MyItemModel { Name = "stefano mostarda", Age = 40 });
  model.Items.Add(new MyItemModel { Name = "Marco leondini", Age = 50 });
  return View(model);
}
  1. Create editor template for int32
@model int

<label>int</label>
<input asp-for="@Model" />
  1. Create editor template for string
@model string

<label>string</label>
<input asp-for="@Model" />
  1. Create Index view
@model WebApplication5.Controllers.MyViewModel

<p>using EditorFor</p>
@Html.EditorFor(m => m.Items[0].Name)
@Html.EditorFor(m => m.Items[0].Age)

<p>using Editor</p>
@Html.Editor("Items[0].FirstName")
@Html.Editor("Items[0].Age")
  1. When the code runs, the calls to EditorFor method work correctly, the calls to Editor method generate empty fields using only the string editor template.

Expected behavior

Editor and EditorFor should produce the same output.

WebApplication5.zip

affected-very-few area-mvc bug help wanted severity-minor

Most helpful comment

@mkArtakMSFT I'd love to do that, but I should investigate the problem (which I think lies in the ViewDataEvaluator class) and, more important, I should download ASP.NET source code, make it build and create the PR. All of these tasks take time. I'll try :D

All 3 comments

@sm15455 thanks for contacting us and thanks for providing a repro project.

We'll have a look at it and get back to you.

Thanks for contacting us, @sm15455.
Would you like to send us a PR for this? We'll happily consider it.

@mkArtakMSFT I'd love to do that, but I should investigate the problem (which I think lies in the ViewDataEvaluator class) and, more important, I should download ASP.NET source code, make it build and create the PR. All of these tasks take time. I'll try :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

UweKeim picture UweKeim  路  3Comments

fayezmm picture fayezmm  路  3Comments

FourLeafClover picture FourLeafClover  路  3Comments

githubgitgit picture githubgitgit  路  3Comments

aurokk picture aurokk  路  3Comments