0

The SDK I am using is .NET 5.0.100 .

I am about to render a RazorView(without a controller) into a string, just like this:

@model Sample.TopicArticleModel
<div>@Model.Title</div>

I do this for I want to use the string.Format to insert it into a string that from the database.

There are so many ways in StackOverFlow while all of them need so much code(just like this:How to Render Partial View into a String).

I wonder if there is an easy way to achieve this with the latest.NET?

Thank you.

Melon NG
  • 2,568
  • 6
  • 27
  • 52

1 Answers1

0

I solved this by a strange way:

async Task<string> PartialToString(string PartialName,object PartialModel)
    {
        StringWriter SW = new System.IO.StringWriter();
        var HtmlContent = await Html.PartialAsync(PartialName, PartialModel);
        HtmlContent.WriteTo(SW, System.Text.Encodings.Web.HtmlEncoder.Default);
        return SW.ToString();
    }
Melon NG
  • 2,568
  • 6
  • 27
  • 52