1

I want to create like this:

[ ['date', 'T1', 'T2', 'T3'], ['2000', 1, 1, 0.5], ['2001', 2, 0.5, 1], ['2002', 4, 1, 0.5], ['2003', 8, 0.5, 1], ['2004', 7, 1, 0.5] ] 

from my model

public partial class AylikOkumaIstatistik { public string sno { get; set; } public string date{ get; set; } public Nullable<decimal> TotalUsageValue { get; set; } public string UsageType { get; set; } // T1,T2,T3 } 

This is possible with using json? If it is possible, is there any code example or tutorial about this topic.How can I convert data that is from database to json?

Thanks.

    2 Answers 2

    1

    This is possible with using json?

    Of course, you could use the object[] type as view model (where each element is itself an object[]):

    public ActionResult Index() { object[] model = new[] { new object[] { "date", "T1", "T2", "T3" }, new object[] { "2000", 1, 1, 0.5 }, new object[] { "2001", 2, 0.5, 1 }, new object[] { "2002", 4, 1, 0.5 }, new object[] { "2003", 8, 0.5, 1 }, new object[] { "2004", 7, 1, 0.5 }, }; return Json(model, JsonRequestBehavior.AllowGet); } 

    As far as converting the data that is in your database to this view model is concerned, well, you will have to give an example of how is your data represented in the database. Ideally provide a hardcoded example of how your model will look like. But it will be a simple matter of projecting your domain model to an object[].

      1

      If you can use Mvc 4? you can add WebApi controller to manage json requests. You will need just to return your entity in WepApi controller method and it will be automatically converted into json object and vice versa. It will work even if you have json objects in post requests body, they will also map to you .net entities and come into controller method as incoming params. If you need i can give any examples you need.

      2
      • I m new about thsi topic and I use mvc3. And Could you give examples?CommentedJul 19, 2012 at 8:46
      • You can use Newtonsoft.Json (you can download it via NuGet). Just using JsonConvert.SerializeObject method you can convert your entity to json object too. Check out related topics: stackoverflow.com/questions/9729220/…. But i realy strongly recomend you to see WebApi as the simplest way if your project is at strart point.CommentedJul 19, 2012 at 8:52

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.