如何将原始 html 文件包含到剃刀视图中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32012178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:04:50  来源:igfitidea点击:

How to include raw html file into razor view

htmlasp.net-mvcrazor

提问by ditoslav

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>)

我有一些代码并想做一些额外的模块化,所以我想将我的组件 Calendar 放在它自己的 html 中(它不是一个完整的 html,而是一个外部标签是<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.htmlto which I'm pointing.

但是他好像不太喜欢。另外,我想路由到我的外部 cshtml 文件,而不是我指向的这个_TSCalendar.html

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

如何将原始 html 文件插入到我的 .cshtml 视图中?

回答by CoOl

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

您可以将任何 html 作为字符串传递给 @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.

您甚至可以传递 selectedDate 之类的值并在 _TSCalendar 部分中使用该值。

回答by Krishan Patel

As CoOl has said - if using MVC I highly recommend using PartialViews - in your case you may just have a simple piece of HTML but if you start using them now you'll realise how powerful it is to be able to call in HTML to your page, and even pass a model/subset of a model into the newly generated view.

正如 CoOl 所说 - 如果使用 MVC,我强烈建议使用 PartialViews - 在您的情况下,您可能只有一段简单的 HTML,但是如果您现在开始使用它们,您将意识到能够将 HTML 调用到您的页面,甚至将模型的模型/子集传递到新生成的视图中。

If you go to add a new View in VS, you will see a checkbox for 'Create as partial' or something along those lines. You can literally just add HTML within tags as you've said above and just call

如果您要在 VS 中添加新视图,您将看到“创建为部分”复选框或类似内容的复选框。您实际上可以像上面所说的那样在标签中添加 HTML,然后调用

 @Html.Partial("_PageToLoad")