SebyD
SebyD

Reputation: 21

Partial view in a non-web application

I built a console application in Visual Studio 2013, that sends email reports daily. I am using a .cshtml template parsed with Razor. Is it possible to use partial views for my main cshtml file? I tried using the syntax:

    @Html.Partial("_partial")

but I get an error ("The name 'Html' does not exist..."). I found information about partial views only with MVC projects. I want to know how and if I can use them in a console application.

I also tried to render the partial view, to a string inside the cshtml main template, but my template will read html markup as literal string. And I can't seem to use HTML helpers outside of MVC.

Thank you in advance.

Upvotes: 1

Views: 651

Answers (2)

SebyD
SebyD

Reputation: 21

I resolved this issue and I will post answer here, in case there are others that would benefit.

I implemented the Partial method that would render the partial view, and then set the new template base.

    public class ExtendedTemplateBase<TModel> : TemplateBase<TModel>
    {
        public string Partial<TPartialModel>(string path, TPartialModel model)
        {
            var template = File.ReadAllText(path);
            var partialViewResult = Razor.Parse(template, model);
            return partialViewResult;           
        }
    }

Upvotes: 1

ChaosPandion
ChaosPandion

Reputation: 78292

You will need to reference System.Web.Mvc.Html and then set the template base.

Razor.SetTemplateBase(typeof(HtmlTemplateBase<>));

Upvotes: 1

Related Questions