2012-06-15 78 views
0

如何從控制器或其他外部類庫中調用Html.Partial()方法?如何渲染局部視圖到字符串?

我需要這樣的東西:

string someView = "SomeView"; 
object someModel = new SomeModel();  
HtmlHelper helper = new HtmlHelper();  
string html = helper.Partial(someView, someModel).ToString(); 

我需要HTML字符串,這個方法返回。

+0

敢肯定你正在做的事情是錯誤的。你想達到什麼目的? –

+0

那麼,你想要的是能夠在視圖之外的模板上使用Razor引擎? –

+0

我不需要使用剃刀引擎。只有mvc項目外的System.Web.Mvc.HtmlHelper類方法。 – Dmytro

回答

2

如果你想渲染串局部視圖,您可以使用此鏈接:Render a view as a string

但我認爲你做錯了什麼......

1

你需要使用Web客戶端下載的HTML頁面,東西這樣的:

string viewUrl = Url.RouteUrl(new { Controller = "Pages", Action = "Index" }); 

WebClient client = new WebClient(); 
client.Encoding = Encoding.UTF8; 
string result = client.DownloadString(new Uri(viewUrl)); 

,那麼你將獲得頁面的HTML