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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:59:51+00:00 2026-05-27T21:59:51+00:00

In my MVC3 solution I’m wondering how to move the logic that returns Json

  • 0

In my MVC3 solution I’m wondering how to move the logic that returns Json out of the controller and into the service layer. Say I have the following action in my controller to get the Json needed for a JQueryUI autocomplete control:

public JsonResult ClientAutocompleteJSON(string term)
{
    NorthwindEntities db = new NorthwindEntities();
    var customers = db.Customers
                .Where(c => c.ContactName.Contains(term))
                .Take(25)
                .Select(c => new
                {
                    id = c.CustomerID,
                    label = c.ContactName,
                    value = c.ContactName
                });

    return Json(customers, JsonRequestBehavior.AllowGet);
}

How would I move this into the service layer? I would prefer not to reference System.Web.MVC in my service layer. I’ve also thought of returning the customers but I’m not sure how to return the anonymous type – would I have to create a class?

  • 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-27T21:59:52+00:00Added an answer on May 27, 2026 at 9:59 pm

    I would not couple your service implementation to a specific (UI) format. It would be better to return a strongly typed customer object and then format this how you want within your Action method.

    // Service method
    
    public IEnumerable<Customer> FindCustomers(string term) {
        NorthwindEntities db = new NorthwindEntities();
        return db.Customers
                    .Where(c => c.ContactName.Contains(term))
                    .Take(25)
                    .ToList();
    }
    
    // Action method
    
    public JsonResult ClientAutocompleteJSON(string term) {
    
        var customers = customerService.FindCustomers(term)
                            .Select(c => new
                            {
                                id = c.CustomerID,
                                label = c.ContactName,
                                value = c.ContactName
                            });
    
        return Json(customers, JsonRequestBehavior.AllowGet);
    }
    

    This code is much more reusable – for example, you could use the same service method to provide a simple HTML search form.

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

Sidebar

Related Questions

I'm working on a asp.net mvc3 solution that has 3 projects Data, Service, and
I have an MVC3 solution where I have 2 repositories & 2 service layers.
I have a solution that has an MVC3 web front end. I have a
I have the first large solution that I am working on using MVC3. I
I'm working on a solution that has a Core project with a DbContext (named
I have business layer and UI layer in separate projects within a same solution.
I am trying to manage roles in an MVC3 app. The idea is that
How to register Controller in MVC3? I have 2 solutions: MvcApplication (MVC 3 Web
I'm running VS2010 Pro SP1 (10.0.40219.1). I copied/pasted a folder into my solution's directory
I've been fiddling with some ASP.NET MVC3 solution structures and I have settled on

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.