Reputation: 5150
Let me start by saying the site already has a Master page so that is not what I am after here.
What I am after is every view page needs some common HTML added to it, is this possible?
so when I click Add View, it creates the view.
But instead of
<h2>Index</h2>
It would be
<my custom set of HTML>
Here is my master layout
<code every page is going to use>
RenderBody()
<more code every page is going to use>
Now my RenderBody() is what brings in my view. Here is what my view needs to look like.
<some code every view will use>
CustomContent
<more code every view will use>
I separated out some of the template html into the view to handle things like breadcrumbs and stuff easier.
Upvotes: 1
Views: 178
Reputation: 1658
Razor/cshtml doesn't have master pages like Web Forms/ASPX. It has Layouts:
You may add your repeating code to your layout and or use partial views. Partialviews are good to break down the code which makes it easier for further development and expansion and to make sure the code doesn't get to messy to a point which is hard to understand it.
if using render partial, you may put it in the layout so that you wont need to repeat it in every page.
if you will not change the code much in future, then layout would be probably a better option as you wouldn't need to have too many files in your project. http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx
You may also use partialViews if you are repeating the code only in some pages using the following code:
http://www.codeproject.com/Tips/617361/Partial-View-in-ASP-NET-MVC-4
Upvotes: 0
Reputation: 1426
You can change the default Scaffolding of the the Visual Studio. Check Modifying the default code generation/scaffolding templates in ASP.NET MVC by Scott Hanselman
When you use the Visual Studio "tooling" (that means dialogs and stuff) to Add View or Add Controller, you're actually executing a T4 template and generating a little bit of code. Where does this start and how can you change it?
Upvotes: 2
Reputation: 6720
Yes it is, you can create a partial view, for example rigth click your shared folder (views->shared), then add view and select partial view, it will open a cshtml file (if c#), then write the content you want in that partial.
now all you have to do to render that html is:
@Html.RenderPartial("MyPartial")
ok lets say you have 3 views, Home, About, and contact, And you want to show a big table with the employees your company have, if you want to keep things DRY (do not repeat yourself), a solution should be to use partial views.
so lets create a partial view that contains this table, add your partial file as shown above, and in this file place your table:
<table>Some Huge content</table>
In your layout you have RenderBody, what this does is render what it is inside your Home, About and Contact views. now what you are going to do is render a partial view inside this views, lets say your home view is:
<h2>Home<h2>
@Html.RenderPartial("MyPartial")
so now you home view will load that huge table too, you can do the same in your About and Contanct views.
hope it helps!
Upvotes: 1