Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am new to asp.net mvc3 and Json
how to create json service in asp.net mvc3
Is it possible to connect ms sql database from json service (with in asp.net mvc3 app)
and can we use this same json service at Iphone client
If anybody knows the answer please give me a sample app for json in asp.net mvc3
Posted

You don't need to create a JSON service in MVC. You just need to return a JsonResult from your controller action method. The ways of connecting to a sql database from a controller action method are exactly the same as other .net sql connections. You should be able to reuse some of the action methods for the Json from other applications.

I'm not going to give you a sample app, but you can see what the manufacturers have in the way of documentation and samples:

www.asp.net/mvc/mvc3[^]
 
Share this answer
 
Comments
member60 17-May-12 0:44am    
my 5!
As jim lahey said "You don't need to create a JSON service in MVC. You just need to return a JsonResult from your controller action method"

The link below will show you how to :
"Building an MVC 3 App with Model First and Entity Framework 4.1"
That helps you, connect to sql server db throw Entity Framework

Then make a Blog Controller contains the code below
C#
public class BlogController : Controller
{
   private BlogModelContainer db = new BlogModelContainer();

   public JsonResult Index()
   { 
      IList<blog> blogs = db.Blogs.ToList();
      return Json(blogs, JsonRequestBehavior.AllowGet);
   }
}

After you call Index Action From Blog Controller (/Blog/Index/) you will get something like this:
HTML
[{"Id":1,"Title":"Title 1","BloggerName":"Blogger Name 1"},{"Id":2,"Title":"Title 2","BloggerName":"Blogger Name 2"},{"Id":3,"Title":"Title 3","BloggerName":"Blogger Name 3"},{"Id":5,"Title":"Title 5","BloggerName":"Blogger Name 5"},{"Id":7,"Title":"Title 7","BloggerName":"Blogger Name 7"},{"Id":8,"Title":"Title 8","BloggerName":"Blogger Name 8"},{"Id":10,"Title":"Title 1","BloggerName":"Blogger Name 10"}]
 
Share this answer
 
v2
Comments
srujana G 25-Apr-12 0:12am    
can I use this service to Iphone
if yes how
Nikfazan 25-Apr-12 6:54am    
Have a look at this: "http://www.microsoft.com/web/post/connecting-the-iphone-with-aspnet-web-pages"
Hope it helps

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900