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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:44:02+00:00 2026-06-13T16:44:02+00:00

I want to return a JSON from ASP.NET MVC ActionResult type method that looks

  • 0

I want to return a JSON from ASP.NET MVC ActionResult type method that looks something like this:

{
     success: true,
     users: [
    {id: 1, FileName: 'Text22'},
    {id: 2, FileName: 'Text23'}
    ]
}

How would I format it? Right now I have something like this

Return Json(New With {Key .success = "true", Key .users = responseJsonString}, JsonRequestBehavior.AllowGet)

Edit: I am using VB.NET but answers in C# are fine too.

  • 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-13T16:44:03+00:00Added an answer on June 13, 2026 at 4:44 pm

    I prefer using ViewModels, rather than manually constructing complex JSON responses. It ensures consistency against all methods that return the data, and is easier to work with strongly typed properties IMHO.

    public class Response
    {
        public bool Success { get; set; }
        public IEnumerable<User> Users { get; set; }
    }
    
    public class User 
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    and then just:

    Response response = new Response();
    response.Success = true;
    // populate the rest of the data
    
    return Json(response);
    

    This also has the advantage of letting you use a base class for every response, if there’s common data like a success status, or error messages.

    public class ResponseBase
    {
        public bool Success { get; set; }
        public string Message { get; set; }
    }
    
    public class UserResponse : ResponseBase
    { 
        IENumerable<User> Users { get; set }
    }
    

    Now, if you have an error:

    return Json(new ResponseBase() { Success = false, Message = "your error" });
    

    or if it succeeds

    return Json(new UserResponse() { Success = true, Users = users });
    

    If you want to manually craft the JSON, then just:

    return Json(new { success = true, users = new[] { new { id = 1, Name = "Alice"}, new { id = 2, Name = "Bob"} } });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send json data from my asp.net MVC controller in C# and
I'm trying to return an error message via Json from ASP.NET MVC controller. I
I want to return a simple json result, something like: { 12323: true }
I've set up DI with Ninject in my ASP.NET MVC application like this Bind<IRepository>().To<XmlDefaultRepository>().WhenInjectedInto(typeof(PageController)).WithConstructorArgument(contentType,
I want my WCF service to return json. I test it on ASP.NET Development
I have a WCF Service that return a json. I want to test it
I am working on an ASP.NET MVC application. This application executes a query via
I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json
I'm a newbie in ASP.net MVC 3. I'm trying to do something trivial, but
I have an ASP.NET MVC 3 (.NET 4) web application. This app fetches data

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.