简体   繁体   中英

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.

In viewModel i have code like following:

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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