[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

convert list to json

I'm  working on a C# asp.net 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  ( http://flexigrid.info )

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?
1 Solution
Fernando SotoCommented:
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


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now