c# - List<T> passed to controller sends empty items -
a partialview contains model foo
list<bar>
. each bar
item contains 2 properties, bara
(string
) , barb
(decimal).
trying render chart in partial view, work need call action on same controller , pass result <img />
element. render chart, need collections of bara
, barb
format data.
i'm trying this:
controller
public void generatechart(list<bar> model) { var chart = new system.web.helpers.chart(400, 200) .addtitle("title") .addseries( name : "name", xvalue : model.select(m => m.bara).toarray(), yvalues : model.select(m => m.barb).toarray()) .write(); }
partial view
routevaluedictionary rvd = new routevaluedictionary(); (int = 0; < model.bars.count; ++i) { rvd.add("model[" + + "]", model.bars[i]); } <img src="@url.action("generatechart", rvd)" />
the problem though model object contains 3 items should contain, these null.
tried use viewbag, this:
viewbag.bara = model.bars.select(m => m.bara).toarray(); viewbag.barb = model.bars.select(m => m.barb).toarray();
with on controller side
public void generatechart() { var chart = new system.web.helpers.chart(400, 200) .addtitle("title") .addseries( name : "name", xvalue : viewbag.bara, yvalues : viewbag.barb) .write(); }
but both arrays null. i've tried few different ideas i'm not able information need.
to triple-check (the data shown fine in view) data correct, changed this:
@{ string[] baras = model.select(m => m.bara).toarray(); decimal[] barbs = model.select(m => m.barb).toarray(); viewbag.baras = baras; // has 3 items expected data viewbag.barbs = barbs; // works } <img src="@url.action("generatechart")" /> string[] baras = viewbag.baras; // assigns null decimal[] barbs = viewbag.barbs; // assigns null
passing complex type actions via request technically bloody thing not know if solution fits needs can follow method below;
your action recieve model serialized string , have deserialize model
public actionresult generatechart(string modelasstring) { list<bar> model = new list<bar>(); model = jsonconvert.deserializeobject<list<bar>>(modelasstring); var chart = new system.web.helpers.chart(400, 200) .addtitle("title") .addseries( name: "name", xvalue: model.select(m => m.bara).toarray(), yvalues: model.select(m => m.barb).toarray()) .getbytes("jpeg"); return file(chart, "image/jpeg"); }
then need call action via querystring ?modelasstring={jsondata}
example url use process data : http://localhost:18681/home/generatechart/?modelasstring=[{%22bara%22:%22baradata%22,%22barb%22:1.1},{%22bara%22:%22baradata2%22,%22barb%22:441.14},{%22bara%22:%22baradata43%22,%22barb%22:44.1}]
you should create <img>
urls via serializing model ready on page's action use <img>
s.
i have tested dummy data can see output below;
ps: creating dummy data used method below;
public actionresult dummydata() { list<bar> model = new list<bar>(); model.add(new bar() { bara = "baradata", barb = 1.1m }); model.add(new bar() { bara = "baradata2", barb = 441.14m }); model.add(new bar() { bara = "baradata43", barb = 44.1m }); return json(model, jsonrequestbehavior.allowget); }
and wonder if more efficient way via request.
hope helps!
Comments
Post a Comment