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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:08:06+00:00 2026-05-19T10:08:06+00:00

I’ve been playing around with ASP.NET MVC for the past few weeks. I’ve got

  • 0

I’ve been playing around with ASP.NET MVC for the past few weeks. I’ve got a simple web application with a form which contains a number of drop down lists.

The items in the drop down lists are stored in a database, and I’m using LINQ to SQL to retrieve them.

My question is – where’s the appropriate place to put this code? From what I’ve read so far, it seems that it’s advisible to keep the Controller ‘thin’, but that’s where I currently have this code as it needs to be executed when the page loads.

Where should I be putting DB access code etc.? I’ve included an excerpt from my controller below.

Thanks.

    public ActionResult Index()
    {
        TranslationRequestModel trm = new TranslationRequestModel();

        // Get the list of supported languages from the DB
        var db = new TransDBDataContext();
        IEnumerable<SelectListItem> languages = db.trans_SupportedLanguages
            .Select(c => new SelectListItem
                {
                    Value = Convert.ToString(c.ID),
                    Text = c.Name.ToString()

                });
        ViewData["SourceLanguages"] = languages;
        ViewData["TargetLanguages"] = languages;
        return View();
  • 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-19T10:08:07+00:00Added an answer on May 19, 2026 at 10:08 am

    Your database access code should be in a repository. Example:

    public interface ITranslationRepository
    {
        Translation GetTransaltion();
    }
    

    and the controller would use this repository:

    public class TransaltionController : Controller
    {
        private readonly ITranslationRepository _repository;
        public TransaltionController(ITranslationRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            // query the repository to fetch a model
            Translation translation = _repository.GetTransaltion();
    
            // use AutoMapper to map between the model and the view model
            TranslationViewModel viewModel = Mapper.Map<Translation, TranslationViewModel>(model);
    
            // pass the view model to the view
            return View(viewModel);
        }
    }
    

    So the basic idea is the following:

    1. The controller queries a repository to fetch a model
    2. The controller maps this model to a view model (AutoMapper is great for this job)
    3. The controller passes the view model to the view
    4. The view is strongly typed to the view model and uses it to edit/display

    As far as the implementation of this repository is concerned feel free to use any data access technology you like (EF, NHibernate, Linq to XML, WCF calls to remote resources over the internet, …)

    There are the following advantages:

    1. The controller logic is completely decoupled from the data access logic
    2. Your controllers can be unit tested in isolation
    3. Your models are not littered with properties that should belong to the UI layer (such as SelectListItem) and thus are reusable across other types of application than ASP.NET MVC.
    4. The view model is a class which is specifically tailored to the needs of the view meaning that it will contain specific formatted properties and the view code will be extremely readable.
    5. Your views are strongly typed => no more ViewData and ugly magic strings
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.