Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have defined few shortCode in Database. When i am displaying in view, ShortCode not replacing with the PartalView defined in the Dictionary.

In Database i am creating a field Description. There i am storing HTML text along with Few Short code. When i display Description in view. I want the short Code to replace with Partial view. Like we do in WordPress/Joomla. In Database i define ShortCode. I want that sortCode replaced with PartialView.

This is the message i am getting on the screen microsoft.AspNetCore.Mvc.ViewFeatures.Buffers.ViewBuffer


What I have tried:

@{
	var input = "<div>Hello This is Same</div><div>{partv _AllPages}</div><div>Some Content</div><div>{partv _AllPost}</div>";
	var replacements = new Dictionary<string, string>
	{ 
		{ "{partv _AllPages}", Html.Partial("_AllPages").ToString()},
		{ "{partv _AllPost}", Html.Partial("_AllPost").ToString()}
	};
	var output = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement.Key, replacement.Value));
}
@Html.Raw(HttpUtility.HtmlDecode(output))
Posted
Updated 9-Aug-22 7:13am

1 solution

The Html.Partial method returns a ViewBuffer instance. That class does not override ToString, so the result of that method will be the full class name.

If you want to render the content to a string, you need to use the WriteTo method[^].
C#
var replacements = new Dictionary<string, IHtmlContent>
{
    ["{partv _AllPages}"] = Html.Partial("_AllPages"),
    ["{partv _AllPosts}"] = Html.Partial("_AllPosts"),
};

string output = input;
foreach (var pair in replacements)
{
    if (!output.Contains(pair.Key)) continue;
    
    string replacement;
    using (var writer = new StringWriter())
    {
        pair.Value.WriteTo(writer, HtmlEncoder.Default);
        replacement = writer.ToString();
    }
    
    output = output.Replace(pair.Key, replacement);
}

NB: Calling HttpUtility.HtmlDecode on the output is almost certainly the wrong thing to do.

I suspect you might be better served using something like this:
Back to Basics: Rendering Razor Views to String in ASP.NET Core - Rick Strahl's Web Log[^]
 
Share this answer
 
v3
Comments
Member 12953926 10-Aug-22 2:59am    
hi thanks for the solution. I have tried it. It gives error. Use of unassigned local variable 'replacement'. Please help

Here what i tried:

@{
var input = "Hello This is Same{partv _AllPages}Some Content{partv _AllPost}";
var replacements = new Dictionary<string,ihtmlcontent>
{
["{partv _AllPages}"] = Html.Partial("_AllPages"),
["{partv _AllPosts}"] = Html.Partial("_AllPosts"),
};
string output = input;
foreach (var pair in replacements)
{
if (!output.Contains(pair.Key)) continue;

string replacement;
using (var writer = new StringWriter())
{
pair.Value.WriteTo(writer, HtmlEncoder.Default);
}

output = output.Replace(pair.Key, replacement);
}
}
@Html.Raw(HttpUtility.HtmlDecode(output))
Richard Deeming 10-Aug-22 7:21am    
Sorry, I missed the replacement = writer.ToString(); line - I've updated my answer.
Member 12953926 10-Aug-22 12:36pm    
Thanks a Lot. It worked.
Member 12953926 2-Sep-22 7:01am    
hi Richard, The above scrip work perfects. Can we cut-shot the script? Because there will be around 200+ partial view in my project. Is there any better way to do?
Member 12953926 13-Dec-22 6:02am    
hi Richard. There is one issue..
in the above script - var input = "Hello This is Same{partv _AllPages}Some Content{partv _AllPost}";
i have removed one partv and its now
var input = "Hello This is Same Some Content";

In the result its calling Html.Partial("_AllPages") and Html.Partial("_AllPosts"). They should be get replaced if its exists in input.

This time in Html.Partial("_AllPages") and Html.Partial("_AllPosts") i have added C# code. Please help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900