开发者

Question on using content from other cshtml pages in razor

开发者 https://www.devze.com 2023-04-02 09:42 出处:网络
I\'ve read a couple of articles on using asp.net mvc3 razor (which I am fairly new to). I have a couple of .cshmtl pages which are like shared content (e.g. header). They are basically just 开发者_JS百

I've read a couple of articles on using asp.net mvc3 razor (which I am fairly new to). I have a couple of .cshmtl pages which are like shared content (e.g. header). They are basically just 开发者_JS百科html pages with one or two divs etc.

To embed this in my main page, do I just use @renderPage("page address"). Do I also need a call to @renderbody? Do I need to specify the/a page in the layout property?

Thanksa


I would put the common elements in a layout (or perhaps a partial view rendered by the base layout). In fact, that's what I did in an application I am now building and it works quite nicely. The one issue is whether or not you need View Model data populated by the controller and passed to that partial view. I did, so I used a base controller and populated the common elements in the view model (all of those also inherited from a base class that had the common properties) and used sections and then in the sections renderered the partial view or not, depending on the view's need.


You can create a Partial View for each of these and call:

@Html.Partial("ViewName")

Or you can use sections, or this article on sections might help too.


As you may or may not know, ASP.NET accepts HTML tagging.

So why not include your .aspx file with the HTML include tag?

Here's how:

<!-- #include virtual="path to file/include-file.html" -->

Ex:

<!--#include virtual="header.aspx"-->

I do this all of the time when writing an ASP.NET website.

Just place it wherever you want the code, from the included page, to show up.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号