C# 如何将局部视图渲染为字符串?

C# 如何将局部视图渲染为字符串?,c#,asp.net,asp.net-mvc,asp.net-mvc-3,partial-views,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,Partial Views,如何从控制器或外部类库中的其他方法调用Html.Partial方法 我需要像这样的东西: string someView = "SomeView"; object someModel = new SomeModel(); HtmlHelper helper = new HtmlHelper(); string html = helper.Partial(someView, someModel).ToString(); 我需要获取此方法返回的html字符串。如果要将部分视图呈现为

如何从控制器或外部类库中的其他方法调用Html.Partial方法

我需要像这样的东西:

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

我需要获取此方法返回的html字符串。

如果要将部分视图呈现为字符串,可以使用以下链接:


但我认为你做错了…

如果你想将部分视图渲染为字符串,你可以使用以下链接:


但是我认为你做错了什么…

你需要使用WebClient下载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

您需要使用WebClient下载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

非常确定您做错了什么。你想实现什么?那么,你想要的是能够在视图之外的模板上使用Razor引擎?我不需要使用Razor引擎。只有System.Web.Mvc.HtmlHelper类方法在Mvc项目之外。不确定您是否理解Partial的功能。很确定您做错了什么。你想实现什么?那么,你想要的是能够在视图之外的模板上使用Razor引擎?我不需要使用Razor引擎。只有System.Web.Mvc.HtmlHelper类方法在Mvc项目之外。不确定您是否理解Partial的功能。