简体   繁体   English

如何从asp.net MVC中的字符串生成局部视图?

[英]How to generate partial views from string in asp.net mvc?

In my project i have a Model that has a string property which contains content with razor standards. 在我的项目中,我有一个具有字符串属性的模型,其中包含带有剃刀标准的内容。 I need to compile the property to showing html result. 我需要编译该属性以显示html结果。

In viewModel i have code like following: 在viewModel中,我有如下代码:

public class PluginContent{
public string Content {private set;get;}
private void FillContent() {
 Content ="@{int count=10; fore (int i=0;i<count;i++) <b>@i.toString() </b>}";
}
public PluginContent(){
FillContent()
}
}

And in razor view i have: 在剃刀视图中,我有:

@model plugincontent

<h3>Your content is: </h3>
@Html.Raw(Model.Content)

I thought Html.Raw cans help me, but it does not. 我以为Html.Raw罐可以帮助我,但事实并非如此。 I need to render this string content like a standard partial views. 我需要像标准局部视图一样呈现此字符串内容。

I don't know how to do what you are looking for exactly. 我不知道该怎么做才是您想要的。 What I would do in a similar situation is write a method that returns the required HTML (instead of Razor to generate the HTML) and call that from the view. 在类似情况下,我将编写一种方法,该方法返回所需的HTML(而不是Razor来生成HTML)并从视图中调用该方法。

Something like: 就像是:

public IHtmlString Content()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++)
{
sb.Append("<b>");
sb.Append(i.ToString());
sb.Append("</b>");    
}

     return new MvcHtmlString(sb.ToString());
}

If you really want to have Razor to generate the HTML look at something like 如果您真的想让Razor生成HTML,请看以下内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM