Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Render Partial View Without Controller

I am having trouble using "RenderPartialViewToString" without a controller class.

I am currently having to create HTML within application start up which requires making a model, making a view and turning the view in to a HTML string.

Within my view it uses HTML Helper function/extension which seems to only be there if a controller is there.

Can anyone shed some light on how I can do this?

like image 594
Lemex Avatar asked Feb 18 '14 13:02

Lemex


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

Razor.Parse is deprecated now. With version 3.5 of the Razor engine you would follow the steps outlined here: https://antaris.github.io/RazorEngine/Upgrading.html

The text below is copied verbatim from the above link:

var result = Razor.Parse(razorTemplate, model, cache_name)

is now either (when the modeltype is known or you want to precompile on startup)

// Once at startup (not required when using an ITemplateManager which knows how to resolve cache_name)
Engine.Razor.AddTemplate(cache_name, razorTemplate)
// On startup
Engine.Razor.Compile(cache_name, typeof(MyModel) /* or model.GetType() or null for 'dynamic'*/)

// instead of the Razor.Parse call
var result = Engine.Razor.Run(cache_name, typeof(MyModel) /* or model.GetType() or null for 'dynamic'*/, model)

or (when you want lazy compilation, like Parse)

// Once at startup (not required when using an ITemplateManager which knows how to resolve cache_name)
Engine.Razor.AddTemplate(cache_name, razorTemplate)

// instead of the Razor.Parse call
var result = Engine.Razor.RunCompile(cache_name, typeof(MyModel) /* or model.GetType() or null for 'dynamic'*/, model)

The semantic equivalent one-liner would be (only to be used to get started with RazorEngine quickly):

// This will just call AddTemplate for you (every time), note that the ITemplateManager has to support AddTemplate
// and it has to handle multiple calls to AddTemplate gracefully to make this work.
// The default implementation will throw an exception when you use the same cache_name for different templates.
var result = Engine.Razor.RunCompile(razorTemplate, cache_name, model.GetType() /* typeof(MyModel) or or null for 'dynamic'*/, model
like image 169
Tom Regan Avatar answered Oct 15 '22 00:10

Tom Regan


You couldn't use html helper without current controller context.Try this extensions for render view into html

public static class RenderViewHelper
{
    public static string RenderPartialToString(string viewPath, object model)
    {
        string viewAbsolutePath = MapPath(viewPath);

        var viewSource = File.ReadAllText(viewAbsolutePath);

        string renderedText = Razor.Parse(viewSource, model);
        return renderedText;
    }

    public static string RenderPartialToString(ControllerContext context, string partialViewName, object model)
    {
        ViewEngineResult result = ViewEngines.Engines.FindPartialView(context, partialViewName);

        var viewData = new ViewDataDictionary() { Model = model };

        if (result.View != null)
        {
            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                using (var output = new HtmlTextWriter(sw))
                {
                    var viewContext = new ViewContext(context, result.View, viewData, new TempDataDictionary(), output);
                    result.View.Render(viewContext, output);
                }
            }

            return sb.ToString();
        }

        return string.Empty;
    }

    public static string MapPath(string filePath)
    {
        return HttpContext.Current != null ? HttpContext.Current.Server.MapPath(filePath) : string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, filePath.Replace("~", string.Empty).TrimStart('/'));
    }
}

First method used razor engine library. Second work with controller context.

like image 44
user3257812 Avatar answered Oct 14 '22 22:10

user3257812