asp.net mvc - How to return Json as part of my view in C# MVC -
my question is: how can combine 2 of these action results one? there seems no point in having first 1 1 line. cant see way out. ideas appreciated. remaining provide background.
i trying , successful in returning partial view json results require using 2 actionresults of same name (one parameter 1 without) achieve this. if continue in manner, have repeat actionresults twice. problem have first action result nothing more literally this:
[httpget] public actionresult myresults() { return partialview(); }
that used return view. within view have jquery/ajax in turn calls action result of same name parameters. action result populates json object parsed , rendered view above table. actionresult work. looks like:
[httppost] public actionresult myresults(datatableprameters param) { //get full list of data meets our needs var fulllist = _myresultsrepository.getlistbyid(id); //count records in set int count = fulllist.count(); //shorten data required page , put object array json var result = r in fulllist.skip(param.idisplaystart).take(param.idisplaylength) select new object[] { r.field1, r.field2, r.field3 }; //return json data datatable return json(new { secho = param.secho, itotalrecords = param.idisplaylength, itotaldisplayrecords = count, aadata = result }); }
those of not familiar it, not aware using di/ioc , brevity have not included details of this. not question. above code used datatables following site:
which quite good. , again, using above, code works well. don't have problems it. know inefficient because loads resultset variable count, skip...take , on again, works , not question.
so again question is, how can combine 2 of them 1 view. there seems no point in having first 1 1 line. cant see way out. ideas appreciated.
assuming separation of post , access not desired 1 action left, wouldn't making "param" optional trick?
public actionresult myresults(datatableprameters param = null) { if(param == null) return partialview(); // repository action... return json([...]); }