Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include raw html file into razor view [duplicate]

I have some code and would like to do some additional modularisation so I would like to have my component Calendar inside its own html (it's not a complete html but one whose outer tags are <div>)

I've tried to do things like

            @RenderSection("~/Views/Home/_TSCalendar.html")

But he seems not to like it. Also, I want to route to my outer cshtml file and not this _TSCalendar.html to which I'm pointing.

How do I insert a raw html file into my .cshtml view?

like image 516
ditoslav Avatar asked Aug 14 '15 14:08

ditoslav


People also ask

What is HTML raw in MVC?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.

What is Razor HTML?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.

What is a Cshtml file?

What is a CSHTML file? A file with . cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser.


1 Answers

Why don't you use partialview?

Anyway what you're trying can be achieved by this -

@Html.Raw(File.ReadAllText(Server.MapPath("~/Views/Home/_TSCalendar.html")))

You can pass any html as a string to @Html.Raw

Update

Just create a new partial view in your Shared folder (if you want to use it across the application), and add the following code in your Views wherever you want to use calendar component. You do not need to explicitly navigate to your partial to access it.

@Html.Partial("~/Views/Shared/_TSCalendar")

You can even pass values like selectedDate and use that value in your _TSCalendar partial.

like image 118
CoOl Avatar answered Sep 29 '22 17:09

CoOl