1

I am trying to dynamically render pages from database. The view looks like this

@model MyModel

@{
    ViewBag.Title = Model.Title;
    Layout = Layout = "~/Views/Shared/_PageInnerLayout.cshtml";
}

@MvcHtmlString.Create(Model.Content)

MyModel has just 3 properties; Id, Title and Content

If the Content has just HTML, the view renders just fine. But in some cases, I need to render partials too. So Content may contain code like

@{
    Html.RenderPartial("_FooterPartial");
}

This does not work. Its being rendered literally, like shown in the image below

Example of rendered page

How do I fix this so that the page is rendered properly?

5
  • you can add flag for example IsPartialContent to model, and if it is true then render partial manualy, also you can see on razorgenerator Commented Dec 7, 2013 at 7:00
  • @Grundy I didn't quite understand your answer. Can you please elaborate? Commented Dec 7, 2013 at 7:08
  • I misunderstood, I thought that the content can only be html or a line renderpartial with no other html code Commented Dec 7, 2013 at 7:12
  • i think template for razorengine what you need Commented Dec 7, 2013 at 8:49
  • 1
    @MvcHtmlString.Create put string in output as is, so if you want put not only html you must process your Model.Content for example in razor engine, and output result string Commented Dec 7, 2013 at 14:05

2 Answers 2

1

The problem is in the difference between Html.RenderPartial and Html.Partial helper methods. RenderPartial directly write the result to HttpContext.Response while Partial return it as a MvcHtmlString. You need to replace the RenderPartial with Partial and maybe the RenderAction with Action.

Edited:

You need to render the content if contains the razor view scripts. If so it means you are actually storing the views or partial views inside the database. You can create a new VirtualPathProvider to help loading the views from the database. For more information see ASP.NET MVC load the Razor views from database and ASP.NET MVC and virtual view

Sign up to request clarification or add additional context in comments.

2 Comments

@Vivek You need to render the content of Model.Content as a view if it contains the razor view scripts, inside the controller's action before pass it to view.
Well, it didn't work. I implemented a VirtualPathProvider and it still wouldn't render Razor Code
0

I tried doing this with VirtualPathProvider, but it didn't work.

Finally what I did was to write the Content from database into a temporary view and then render the view. It worked

public ActionResult DynamicPage(int id) {
  var dynamicPage = new PagesContext().Pages.FirstOrDefault(s => s.Id == id);

  string webroot = Server.MapPath("~").TrimEnd('\\');
  string fileName = webroot + "\\Views\\\\Solutions\\Temp.cshtml";

  System.IO.File.WriteAllText(fileName, dynamicPage.Content);
  return View("Temp");
}

Here, the Content property of the dynamicPage containts HTML(including Razor code)

2 Comments

I beg to disagree, this solution has performance issue and it is not thread safe also. The multiple requests executing in the multiple threads try to write to a single file leads to the synchronization issues.
@KambizShahim Yes. That is an issue. Any suggestions please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.