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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:14:10+00:00 2026-05-20T05:14:10+00:00

I am implementing, for the first time, Json.NET for my ASP.NET MVC2 site. My

  • 0

I am implementing, for the first time, Json.NET for my ASP.NET MVC2 site.

My original code looked like this:

[HttpPost]
public ActionResult FindMe(string searchFirstName, string searchLastName)
{
    this.searchFirstName = searchFirstName;
    this.searchLastName = searchLastName;
    IEnumerable<HomePageUser> results = doSearch();
    bool success = (results.Count() == 0) ? false : true;

    return Json(new
    {
        success = success,
        results = results
    });
}

The results were problematic due to the fact that one of the items in the results set is an Enum and I really want the Text value, not the numeric one. Additionally, the date format is a problem.

So, I then found Json.NET and changed my code to this:

[HttpPost]
public JsonNetResult FindMe(string searchFirstName, string searchLastName)
{
    this.searchFirstName = searchFirstName;
    this.searchLastName = searchLastName;
    IEnumerable<HomePageUser> results = doSearch();
    bool success = (results.Count() == 0) ? false : true;

    JsonNetResult jsonNetResult = new JsonNetResult();
    jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
    jsonNetResult.Data = results;// how to add success true/false info?

    return jsonNetResult;
}

This fixes the above two issues, but now I’m wondering how to make this seamless with my existing javascript code that was expecting json that looked like:

{
    "success":true,
    "results":[{
        "UserId":545,
        "FirstName":"Scott",
        "LastName":"Roberson"}]
}

This allowed me to first test for response.success before proceeding to write out the answer, versus moving into the section to handle errors.

So, my question is how do I add a top-level success json node to live beside the results node?

Thanks.

UPDATE:

As is often the case, the act of writing out a question sparked an idea in one of those Doh! moments.

if I add:

var returnPackage = new { success = success, results = results};

then add this to the jsonNetResult.Data like so:

jsonNetResult.Data = returnPackage;

It works perfectly.

Thanks, anyway.

Final code:

[HttpPost]
public JsonNetResult FindMe(string searchFirstName, string searchLastName)
{
    this.searchFirstName = searchFirstName;
    this.searchLastName = searchLastName;
    IEnumerable<HomePageUser> results = doSearch();
    bool success = (results.Count() == 0) ? false : true;

    var returnPackage = new { success = success, results = results};

    JsonNetResult jsonNetResult = new JsonNetResult();
    jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
    jsonNetResult.Data = returnPackage;

    return jsonNetResult;
}
  • 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-20T05:14:10+00:00Added an answer on May 20, 2026 at 5:14 am

    Here’s what I do in my MVC/Ajaxy apps. Define a new class:

    public class JsonResultData
    {
        private bool _success = true;
        public bool Success
        {
            get { return _success; }
            set { _success = value; }
        }
    
        public object Value { get; set; }
        public List<string> Errors { get; set; }
    
        public JsonResultData()
        {
            this.Errors = new List<string>();
        }
    }
    

    Then set your return data value to:

    jsonNetResult.Data = new JsonResultData { Value = results };
    

    JsonResultData.Success property is what you want to test in the json response first. It defaults to true, but if something goes wrong in your server-side method, you set it to false and add your error message, like this:

        catch (Exception ex)
        {
            List<string> errors = new List<string>();
            errors.Add(ex.ToString());
            jsonNetResult.Data = new JsonResultData { Success = false, Errors = errors };
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time implementing In App Purchases. During development and testing of
It's almost the first time I'm implementing a table view in my code. I
Got this error for the first time, I have looked around and cannot find
I am first time implementing json in my app. and in my app I
I'm implementing the specification pattern. The NotSpecification seems simple at first: NotSpecification.IsSpecialCaseOf(otherSpecification) return !this.specification.isSpecialCaseOf(otherSpecification)
implementing publishActivity in PHP using the REST API using this code: $activity = array(
I'm implementing a treeView for the first time in Google Apps Script and running
I'm implementing django-haystack for the first time and need to make a geo-spatial search.
I am currently implementing Facebook Login on my website for the first time and
I'm implementing stateless session bean ejb3 in glassfish server using netbeans. First time, it

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.