Fraz Sundal
Fraz Sundal

Reputation: 10448

Can i convert JSONResult to IEnumerable list in C#

I have a function that send a JSONResult, Now i want to use that function in C# and convert that JSONResult to IEnumerable so i can iterate on that result and pass that data to a SelectList function. Can i do this and how? Im using Asp.Net MVC, JQuery and C#

Upvotes: 1

Views: 2164

Answers (2)

Ali Osman Yavuz
Ali Osman Yavuz

Reputation: 417

Also it can be done in this way.

var data = GetJsonResultData(); //call to JsonResult method.

var datastr = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(data.Data); // convert json object to string.

var dataclass = Newtonsoft.Json.JsonConvert.DeserializeObject<List<modeldto>>(datastr ); // string json data to class list

Upvotes: 0

balexandre
balexandre

Reputation: 75093

why not:

public myObject GetMyObject()
{
    myRepository db = new myRepository();
    return db.ListAllStuff();
}

public JsonResult GetMyJSON()
{
    return Json(GetMyObject(), JsonRequestBehavior.AllowGet);
}

public List<SelectList> GetMyEnumerable()
{
    return this.GetMyObject().ToList();
}

and you are reusing everything.

Upvotes: 1

Related Questions