1

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.

leppie
  • 115,091
  • 17
  • 196
  • 297
SebyD
  • 21
  • 6
  • What is your goal with Partial methods? Maybe there is an another solution for your goal. – Uğur Aldanmaz Sep 22 '14 at 14:43
  • Maybe what you need is to render a partial view to a string. In this case take a look to this thread: http://stackoverflow.com/questions/483091/render-a-view-as-a-string – simoneL Sep 22 '14 at 14:45
  • @UğurAldanmaz: I want to be able to reuse some parts of the view. I already have too much code in one cshtml file, so I thought it would be easier to group parts of code in separate partial views. – SebyD Sep 22 '14 at 15:48
  • What is exist in your partial views? Do you use a dynamic data? Probably yes, so what kind of data? – Uğur Aldanmaz Sep 22 '14 at 18:09
  • I use a model object in my parent view. And I want to use the same in my partial view, if that's possible. – SebyD Sep 30 '14 at 06:55

2 Answers2

1

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

Razor.SetTemplateBase(typeof(HtmlTemplateBase<>));
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
1

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;           
        }
    }
RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
SebyD
  • 21
  • 6