Currently I try to convert List object to Json string, this drive me crazy on my ASP.net MVC project.
Model:
public class PagerBase<T>:List<T> where T:EntityBase
{
public int totalpage {get;set;}
public int pageindex {get;set;}
public int pagesize {get;set;}
public PagerBase(IEnumerable<T> source, int totalpage , int pageindex ,int pagesize)
{
this.totalpage = totalpage ;
this.pageindex = pageindex;
this.pagesize = pagesize;
this.AddRange(source);
}
}
My Action like this
public JsonResult GetClient()
{
int pagesize=20;
int pageindex1;
var providers = getclient(20,1);
var totalpage= gettotalpage(20);
var pagerclient=new PagerBase<Client>(providers,totalpage,pageindex,pagesize);
JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsonstring=serializer.Serialize(pagerclient);
JsonResult jr = Json(new
{
Html = jsonstring,
Message = "Ok"
}, JsonRequestBehavior.AllowGet);
return jr;
}
I want result at least include totalpage, pagesize, pageindex but actually I got empty string if there is no client.
JsonResultlibrary (other than maybe because it's insystem.web.mvcbut that's not a good reason). If you're open to using json.NET instead I will provide an answer but thatJsonResultclass is just a joke and I won't bother trying to correct any code that uses it :p