convert list to json

Posted on 2014-08-18
Last Modified: 2014-09-03
I'm  working on a C# mvc 5 application and using Visual Studio 2013.
I'm using database first entity model and Entity Framework 6.

I'm using the Customers and Orders tables from the Northwind database.
My entity model is called northwindEF.edmx and contains a stored procedure called GetCustomerOrders.

Visual Studio  generated a function for my stored procedure called GetCustomerOrders_Result

So right now in my Models folder  of my application along with northwindEF.edmx  I also have a class called NorthwindModel.cs

The code for NorthwindModel.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;

namespace NorthwindSample.Models
    public class NorthwindModel
        // an instance called customerordersdb of the NorthwindEntities
        NorthwindEntities customerordersdb = new NorthwindEntities();

        // a function called GetCustOrders that calls the GetCustomerOrders_Result collection and returns a list
        public List<GetCustomerOrders_Result> GetCustOrders()
            return customerordersdb.GetCustomerOrders().ToList();

Open in new window

So I tried binding that function to a HTML Helper MVC Web Grid in my view and that function works fine.

No I'm trying to bind this function to a jquery Flexigrid  ( )

In order to be able to bind it to flexigrid I have to convert  my list function shown above to a function that returns a json version of my list.   Because I have to feed flexigrid json type.

Anyone know the syntax for a json function that will convert my list function above to json data type?
Question by:maqskywalker
    1 Comment
    LVL 62

    Accepted Solution

    Hi maqskywalker;

    This should do what you want.

    // You will need to add System.Web.Extensions.dll 
    // If not already added.
    using System.Web.Script.Serialization;
    // Create a serializer
    private JavaScriptSerializer objJSSerializer = new JavaScriptSerializer();
    // When you need to a Json you can do this with the instance on the
    // object to be serialized as the parameter
    string jsonStr = objJSSerializer.Serialize(the.Net.Instance.Object);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now