An ActionResult to return JSON from ASP.NET MVC to the browser using Json.NET. Taken from http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx
Inheritance: System.Web.Mvc.ActionResult
Exemplo n.º 1
0
        public void ResultReturnJsonStringWhenDataHasOneProperty()
        {
            var sampleClass = new SampleClassWithOneJsonProperty();

            var result = new JsonNetResult(sampleClass);

            Assert.AreEqual("{\"Name\":\"SampleName\"}", result.JsonResultString);
        }
Exemplo n.º 2
0
        public void ResultReturnJsonStringWhenDataHasSubValues()
        {
            var sampleClass = new SampleClassWithSubJsonPropert();

            var result = new JsonNetResult(sampleClass);

            Assert.AreEqual("{\"Country\":\"USA\",\"FavoriteCity\":\"SampleCity\"}", result.JsonResultString);
        }
Exemplo n.º 3
0
        public void ResultReturnJsonStringWhenDataHasMultipleProperties()
        {
            var sampleClass = new SampleClassWithMultipleJsonProperties();

            var result = new JsonNetResult(sampleClass);

            Assert.AreEqual("{\"Name\":\"SampleName\",\"FavoriteNumber\":42}", result.JsonResultString);
        }
Exemplo n.º 4
0
        public void ResultReturnsDataWasNullForNullData()
        {
            var result = new JsonNetResult(null);

            Assert.AreEqual("[Data was Null]", result.JsonResultString);
        }