Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:18:19+00:00 2026-06-09T12:18:19+00:00

I am aware that complex types are not usually rendered when using EditorForModel but

  • 0

I am aware that complex types are not usually rendered when using EditorForModel but I am using a custom object template that does not do the check and calls Html.Editor for every property including complex types.

Unfortunately, whilst I can see the correct TemplateHint value for the property within the object template, the Editor call doesn’t seem to use it and the built in collection template is used instead.

My object template is basically this:

@foreach (var property in ViewData.ModelMetadata.Properties.Where(x => x.ShowForEdit))
{
  @Html.Editor(property.PropertyName)
}

If I force the use of the template by passing the name to the Editor call then the ModelMetadata is empty in the template.

Is this a bug / are there any workarounds?

Some more info:

So my view model contains the following:

[ACustomAttribute("Something")]
public IEnumerable<int> SelectedOptions { get; set; }

The attribute implements IMetadataAware and adds some stuff to the AdditionalValues Collection of the ModelMetadata as well as setting the TemplateHint. I can read this data from the object template but not from my custom template.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:18:21+00:00Added an answer on June 9, 2026 at 12:18 pm
    @foreach (var property in ViewData.ModelMetadata.Properties.Where(x => x.ShowForEdit))
    {
        if (!string.IsNullOrEmpty(property.TemplateHint))
        {
            @Html.Editor(property.PropertyName, property.TemplateHint)
        }
        else
        {
            @Html.Editor(property.PropertyName)
        }
    }
    

    But please note that if you don’t rely on the established conventions for resolving templates for complex collection types (a.k.a ~/Views/Shared/EditorTemplates/NameOfTheTypeOfCollectionElements.cshtml) and have used an UIHint on your collection property:

    [UIHint("FooBar")]
    public IEnumerable<FooViewModel> Foos { get; set; }
    

    then the ~/Views/Shared/EditorTemplates/FooBar.cshtml editor template must be strongly typed to IEnumerable<FooViewModel> and not FooViewModel. So be careful, if this is your case, it’s up to you to loop inside this custom template if you want to get to individual items of the collection. It will no longer be ASP.NET MVC that will automatically loop for you and invoke the editor template for each element.


    UPDATE:

    Still can’t repro your issue.

    Custom attribute:

    public class ACustomAttribute : Attribute, IMetadataAware
    {
        private readonly string _templateHint;
        public ACustomAttribute(string templateHint)
        {
            _templateHint = templateHint;
        }
    
        public void OnMetadataCreated(ModelMetadata metadata)
        {
            metadata.AdditionalValues["foo"] = "bar";
            metadata.TemplateHint = _templateHint;
        }
    }
    

    Model:

    public class MyViewModel
    {
        [ACustom("Something")]
        public IEnumerable<int> Foos { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MyViewModel
            {
                Foos = Enumerable.Range(1, 5)
            };
            return View(model);
        }
    }
    

    View (~/Views/Home/Index.cshtml):

    @model MyViewModel
    
    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
    }
    

    Editor template for the object type (~/Views/Shared/EditorTemplates/Object.cshtml):

    @foreach (var property in ViewData.ModelMetadata.Properties.Where(x => x.ShowForEdit))
    {
        if (!string.IsNullOrEmpty(property.TemplateHint))
        {
            @Html.Editor(property.PropertyName, property.TemplateHint)
        }
        else
        {
            @Html.Editor(property.PropertyName)
        }
    }
    

    Custom editor template (~/Views/Shared/EditorTemplates/Something.cshtml):

    @model IEnumerable<int>
    
    <h3>
        @ViewData.ModelMetadata.AdditionalValues["foo"]
    </h3>
    
    @foreach (var item in Model)
    {
        <div>
            @item
        </div>
    }
    

    Result:

    enter image description here

    So as you can see the additional metadata we added is shown in the template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that MD5 hashes are not advisable for security any more but
I'm aware that I can check an ActiveRecord through the rails console but it
I am aware that by default Java does not have the so-called eval (what
I aware that this will be a less programming question, but still... How can
I am aware that I can set null like string val = null; But
I'm aware that this is possible with the os module's os.system(color) function, but this
I am aware that I can access all external links on a page using
I'm aware that map is not prepared to be sorted. It's heavily optimized for
I have sqlite3.dll but I'm assuming it's far more complex than simply including that
I'm aware that the question is more a question to-be-discussed than to-be-answered, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.