CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: HTML

Testing out media types with +json

195 Views
Copy Code Show/Hide Line Numbers
[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
 
        return View();
    }
 
    public ActionResult About()
    {
        return View();
    }
 
    public ActionResult GetJson()
    {
        return new JsonResult()
            {
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                ContentType = "application/vnd.contoso.contactmanager+json",
                Data =
                    new[]
                        {
                            new Contact {First = "Glenn", Last = "Block"},
                            new Contact {First = "Darrel", Last = "Miller"}
                        }
            };
    }
}
 
public class Contact
{
    public string First { get; set; }
    public string Last { get; set; }
}
 
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
 
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
    About Us
</asp:Content>
 
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p id="contacts">
    </p>
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script type="text/javascript">
        $(document).ready(function () {
            $.getJSON("/home/GetJson", function (data) {
                $.each(data, function (i, contact) {
                    $("#contacts").append(contact.First + " " + contact.Last).append("<BR/>");
                });
            });
 
        });
    </script>
</asp:Content>
 
 
by Glenn Block
  July 11, 2010 @ 11:28pm

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate