c# 4.0 - Get html from MVC 4 view into a string -
i trying use accepted answer this question.
it seems looking for, have problem. don't know how call it. have far:
first copying code solution mentioned:
public string tohtml(string viewtorender, viewdatadictionary viewdata, controllercontext controllercontext) { var result = viewengines.engines.findview(controllercontext, viewtorender, null); stringwriter output; using (output = new stringwriter()) { var viewcontext = new viewcontext(controllercontext, result.view, viewdata, controllercontext.controller.tempdata, output); result.view.render(viewcontext, output); result.viewengine.releaseview(controllercontext, result.view); } return output.tostring(); }
this have:
string viewtorender = "..."; int data1 = ...; int data2 = ...; system.web.mvc.viewdatadictionary viewdata = new system.web.mvc.viewdatadictionary(); viewdata.add("data1",data1); viewdata.add("data2",data2); string html = tohtml(viewtorender, viewdata, ?????)//here problem.
what should pass in controllercontext parameter?
you can create base controller extends controller , use above function in base controller , other controller extends base controller able use it. controllercontext must used as
request.requestcontext
and hence basecontroller like
public class basecontroller: controller { //your function here }
and tohtml() function be
protected virtual string tohtml(string viewtorender, viewdatadictionary viewdata ) { var controllercontext=request.requestcontext; var result = viewengines.engines.findview(controllercontext, viewtorender, null); stringwriter output; using (output = new stringwriter()) { var viewcontext = new viewcontext(controllercontext, result.view, viewdata, controllercontext.controller.tempdata, output); result.view.render(viewcontext, output); result.viewengine.releaseview(controllercontext, result.view); } return output.tostring(); }
and on using base controller
public class mycontroller: basecontroller { //tohtml(...); }
Comments
Post a Comment