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 9084353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:58:42+00:00 2026-06-16T20:58:42+00:00

As you might know, ASP.NET MVC has support for custom view overrides for model

  • 0

As you might know, ASP.NET MVC has support for custom view overrides for model fields within views. There are special folders in the Views folder called Views\Shared\EditorTemplates, Views\Shared\DisplayTemplates and so on, and these folders can contain files like Views\Shared\EditorTemplates\String.cshtml, which will override the default view used when calling @Html.EditorFor in a view with a model with a String field.

What I want to do is to use this functionality for a custom kind of templates. I want to have a folder like Views\Shared\GroupTemplates that may contain e.g. Views\Shared\GroupTemplates\String.cshtml and Views\Shared\GroupTemplates\Object.cshtml, and I want to create a HtmlHelper method that allows me to call for example Html.GroupFor(foo => foo.Bar), which will load the template in String.cshtml if Bar is a String property, and the template in Object.cshtml otherwise.


Full example of the expected behavior; if Views\Shared\GroupTemplates\String.cshtml contains this:

@model String
This is the string template

… and Views\Shared\GroupTemplates\Object.cshtml contains this:

@model Object
This is the object template

I have a model like:

class Foo
{
    public bool Bar { get; set; }
    public String Baz { get; set; }
}

And a view in Views\Foo\Create.cshtml like:

@model Foo
@Html.GroupFor(m => m.Bar)
@Html.GroupFor(m => m.Baz)

When I render the view Create.cshtml, the result should be this:

This is the object template
This is the string template

How should GroupFor be implemented?

  • 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-16T20:58:44+00:00Added an answer on June 16, 2026 at 8:58 pm

    The thing is that you can easily specify your view location like that

    html.Partial("~/Views/Shared/GroupTemplates/YourViewName.cshtml");
    

    or even override default behaviour by implementing custom view engine, for an example see this blog A Custom View Engine with Dynamic View Location

    But you also want to reuse the logic which determines the view name based on its model type. So that if a view with String name doesn’t exist an Object view is picked up. Which means going through parent classes.

    I’ve had a look how EditorFor is implemented:

    public static MvcHtmlString EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
    {
        return html.TemplateFor<TModel, TValue>(expression, null, null, DataBoundControlMode.Edit, null);
    }
    

    It uses TemplateFor method which is internal and you can’t just reuse it.

    So I can only see of two options:

    1. Implement you custom logic by checking if a view file with a correct name exists by trying model type name and its parent classes. And if you find a proper view just use Partial extension in your helper.
    2. Try to use reflection to call internal method. But this approach is more like a hack than a solution.

    Hope it helps!

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

Sidebar

Related Questions

In an ASP.NET MVC application (which has very little chance of having its view
I would like to create an ASP.NET MVC web application which has extensible logic
Just some architectural question: I use ASP.net MVC and exclusively rely on Strongly-Typed Views
I have an ASP.NET MVC control which has to do some work to format
So a request is made to an ASP.net MVC controller/action. This has an associated
I'm just starting to learn ASP.NET MVC and I'd like to know how I
I am using ASP.NET MVC 3 . I get my view's data in the
I'm creating a web service (asmx) using asp.net mvc 3 and it has a
I've been looking a lot recently as best practices within the ASP.NET MVC framework,
I am using ASP.Net MVC 3 Razor view engine. I have a requirement to

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.