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

  • Home
  • SEARCH
  • 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 976701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:47:23+00:00 2026-05-16T03:47:23+00:00

Is it a code smell to have to following pattern, given the following code

  • 0

Is it a code smell to have to following pattern, given the following code (highly simplified to get straight to the point) ?

The models :

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Category Cat { get; set; }
}

class Category
{
    public int Id { get; set; }
    public string Label { get; set; }
}

The view to edit a Product :

<% =Html.EditorFor( x => x.Name ) %>
<% =Html.EditorFor( x => x.Category ) %>

The EditorTemplate for Category

<% =Html.DropDownList<Category>() %>

The HtmlHelper method

public static MvcHtmlString DropDownList<TEntity>(this HtmlHelper helper)
    where TEntity : Entity
{
    var selectList = new SelectList(
        ServiceLocator.GetInstance<SomethingGivingMe<TEntity>>().GetAll(), 
        "Id", "Label");

    return SelectExtensions.DropDownList(helper, "List", selectList, null, null);
}

For information, the real implementation of the helper method takes some lambdas to get the DataTextField and DataValueField names, the selected value, etc.

The point that bothers me is using a servicelocator inside the HtmlHelper. I think I should have a AllCategories property in my Product model, but I would need to be populated in the controller every time I need it.

So I think the solution I’m using is more straightforward, as the helper method is generic (and so is the modelbinder, not included here). So I just have to create an EditorTemplate for each type that needs a DropDownList.

Any advice ?

  • 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-05-16T03:47:23+00:00Added an answer on May 16, 2026 at 3:47 am

    IMHO I’d leave it the way it is, have the same thing in another project.

    BUT the service location bothered me as well so for another project I made this part of an ActionFilter which scans a model, finds all the anticipated dropdowns and does a batch load into ViewData. Since the ServiceLocator or Repository/Context/whatever is already injected into the Controller you don’t have to spread your service location all over the place.

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        foreach( var anticipated in SomeDetectionMethod() )
        {
              var selectList = new SelectList(
        ServiceLocator.GetInstance<SomethingGivingMe<TEntity>>().GetAll(), 
        "Id", "Label");
    
             ViewData["SelectList." + anticipated.Label/Name/Description"] = selectList;
        }
    }
    

    In the view you can then make a helper to load up those dropdowns via a custom editor template or other method.

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

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can simply convert the arrays into vectors before passing… May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer Set UIView.exclusiveTouch. May 16, 2026 at 8:37 am
  • Editorial Team
    Editorial Team added an answer You should retain the view controller in your HTTP request… May 16, 2026 at 8:37 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm stuck with the following scenario. It has a code smell, that I'm not
Consider the following code: partial class OurBusinessObject { partial void OnOurPropertyChanged() { if(ValidateOurProperty(this.OurProperty) ==
Consider the following code: class Program { static void Main(string[] args) { Department deathStar
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
I have the following try-catch statement and I do not want to not throw
I have run across a bunch of code in a few C# projects that
I have a table called Registrations with the following fields: Id DateStarted (not null)
I have a situation where my regular expressions compile extremely slowly on Windows Server
I currently work on a small application with models, mappers and controllers. My question
I've decoupled events in this WPF application in the following way. What is the

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.