c# - MVC Json to HTML table -
i trying pass serialized json string (serialized using jsonconvert.serializeobject) html table. call values inside json string table this: "model.jsonvalue". not sure how make variables out of json string.
my json string:
{ "stats": { "global": { "cache": { "misses": "5" }, "download": { "total-downloaded": "500" }, "error": { "config-failed": "50", "retries": "20" }, "instance": { "resets": "2016-06-23 09:45:07" }, "servers": { "server-in-config": "1", "servers-running": "1", "servers-relays": "1" } }, "servers": { "12345": { "uptime": "0d, 18:01:30", "retries": "0", "download-size": "54664", "server-restart": "1", "download-time": "1", "start-time": "2016-06-23 09:45:07", "logic-time": "123", "heartbeat": "1", "logic-retry": "0" }, "44444": { "start-time": "2016-06-23 09:45:07", "download-time": "1", "logic-time": "123", "download-size": "54664", "server-restart": "1", "logic-retry": "0", "uptime": "0d, 18:01:30", "heartbeat": "1", "retries": "0" } } } }
in controller serialized follow:
string json = system.io.file.readalltext(@path); string jsonoutput = jsonconvert.serializeobject(json);
...and have controller values want pass tables follow:
public class stats { public global global { get; set; } public list<server> servers { get; set; } } public class global { public cache cache { get; set; } public download download { get; set; } public error error { get; set; } public instance instance { get; set; } public servers servers { get; set; } }
now know there lots of articles explaining how (i did google) not want use ajax, javascript, knockout or scripts. plain mvc.
hope can assist new mvc , json.
if want plain asp mvc don't convert data file json. load statically typed model (global class) , pass view controller, i.e.:
controllers\datacontroller.cs:
public class datacontroller : controller { public actionresult index() { string data = system.io.file.readalltext(@path); var model = convertdatatomodelsomehow(data); //model variable type: global return view(model); } }
then can make view , use model - views\data\index.cshtml:
@model global //your model type passed view <table> <thead> <tr> <th>server name</th> </tr> </thead> <tbody> @foreach(var item in model.servers) //if global.servers collection { <tr> <td>@item.name</td> //if element has name property </tr> } </tbody> </table>
Comments
Post a Comment