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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:09:36+00:00 2026-05-17T19:09:36+00:00

I am creating a store with ASP.NET 4.0 MVC and C# and am fairly

  • 0

I am creating a store with ASP.NET 4.0 MVC and C# and am fairly new to it.

I have come to creating the View page that displays the products within a certain category.

On the specific category page I want to have the product list and also want the category name with its relevant description taken from the database.

Currently the way I have done this is to have two methods in my Repository:

  1. Retrieve the list of products for a specific category with a string
  2. Retrieve the specific category with a string

I then use both these in one ActionResult and then pass them to the view.

Is there a way that I can retrieve both the product list and category name, description etc from 1 method call to the database or have i done it correctly?

Thank you for any help in advance.

My code is as follows:

StoreRepository

public class StoreRepository : topsports.Models.IStoreRepository
{
    private topsportsEntities db = new topsportsEntities();

    public IQueryable<Product> FindProductsByCategory(string c)
    {
        var products = from p in db.Products
                       where p.Category.Name == c
                       select p;

        return products;
    }

    public Category FindCategory(string c)
    {
        return db.Categories.SingleOrDefault(cg => cg.Name == c);

    }
}

IStoreRepository

public interface IStoreRepository
{
    IQueryable<Product> FindProductsByCategory(string c);
    Category FindCategory(string c);

}

StoreController

 public class StoreController : Controller
{
    IStoreRepository storeRepository;

    public StoreController()
        : this(new StoreRepository())
    {
    }
    public StoreController(IStoreRepository repository)
    {
        storeRepository = repository;
    }

    public ActionResult Index(string c)
    {
        var category = storeRepository.FindCategory(c);

        var products = storeRepository.FindProductsByCategory(c).ToList();

        var viewModel = new StoreViewModel
        {
            Products = products,
            Category = category

        };

        return View(viewModel);
    }
}

StoreViewModel

public class StoreViewModel
{
    public List<Product> Products { get; set; }
    public Category Category { get; set; }

}

Category.aspx

<h2><%: Model.Category.Name %></h2>

<p><%: Model.Category.Description %></p>


<ul>
    <% foreach (var item in Model.Products) { %>
        <li>
            <%: item.Name %>, 
            <strong><%: item.Description %></strong>
        </li>
    <%} %>
</ul>
  • 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-17T19:09:36+00:00Added an answer on May 17, 2026 at 7:09 pm

    The purpose of repositories is to decouple your data access layer from your business logic. When you choose to retrieve products through the category entity, you’re depending on lazy loading which is an implementation detail of the entity framework. When you would e.g. later on decide to switch to a different data access layer (hand created queries e.g.), it could be you would not have this facility anymore.

    A second issue is that when you put a lot of functionality into a single repository method, it becomes unclear what the responsibility of this method is. As @Andrew Barber describes, yes, you will get a lot of small methods. These can then be combined to produce useful functionality. When you would choose to create bigger methods that return more results, you get another problem. When the method that returns e.g. three or four data sets, you get the issues that when you need only on or two of two of these data sets, you either are going to create a new method which does less as the original one, or you are going to run four queries where one or two would have been enough.

    The small methods of the repository are meant to produce meaningful results when composed in a larger whole. Many methods is not necessarily a problem. Your code looks fine :).

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

Sidebar

Related Questions

We're creating an ASP.Net MVC site that will need to store 1 million+ pictures,
I have an ASP.net page that is creating a service reference to a WCF
I am new to ASP.NET. I am creating a website in which i have
I'm creating a discussion forum (of sorts) in ASP.NET MVC, and I have a
I am currently creating an e-commerce site using C# ASP.NET MVC and have just
I have installed MVC 3 and now I'm trying to follow these steps: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1
I have a project using ASP.Net MVC 3 and now I'm creating a report
I have an ASP.Net MVC 3 app that I've developed which uses RavenDB Embedded
Working inside the context of an ASP.NET application I am creating a page that
I am creating a website in asp.net but i have some issue.. I have

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.