Reputation: 85765
I just got a couple questions about asp.net mvc 3.
I was reading about layouts with razor .
So the article I think is from like preview 1 and I am using the final version of 3.0.
When I make a 3.0 project and look at _Layout.cshtml it has
<title>@ViewBag.Title</title>
What is ViewBag? I heard it is like equivalent to ViewData.
If what I heard is right why are they using it like this? In the article I listed they used
<title>@View.Title </title>
So is the above obsolete or is it a different way of doing it?
With _ViewStart.cshtml if you put one of these in the shared folder and then one in the home views folder. The one in the home views one will trump the shared one?
Can you have _ViewStart.cshtml in the shared folder?
In the aspx engine you had master pages with content tags. @Sections seem to be the equivalent to it. However if you only need one content tag(ie for the body) you just use @RenderBody?
Is @RenderPage used for loading partial views or is it used for something different?
What is the difference from using @RenderSection Optional and just not putting anything between the content tags if you were using the aspx engine?
What happens if you want the stuff in the @RenderSection to stored in a partial view. Can you still make it optional?
Thanks
Upvotes: 2
Views: 898
Reputation: 1038770
In prerelease versions ViewBag
was called View
. In the RTM it is ViewBag
. ViewBag is equivalent to ViewData
with the only difference that it relies on .NET 4.0 dynamic properties meaning that instead of ViewData["Title"]
you could write ViewBag.Title
. This being said you should use none of those but have strongly typed views.
It is obsolete and it won't compile in ASP.NET MVC 3 RTM
This will depend whether you put it after or before the @RenderBody()
call_ViewStart
execution walks up the folder structure of whatever is the main view being rendered. If the view gets found in the Home folder Home\_ViewStart will be executed. If the view gets found in the Shared folder Shared\_ViewStart will be executed.
No
Yes, but it will only get executed if the view being executed is located in the Shared folder.
Yes
@RenderPage
is the same as @Html.Partial
allowing you to include a partial view. It's a matter of personal preference. It comes from WebPages whereas Html.Partial is an extension method that has been around since the early versions of MVC.
Clarification: In MVC you should only use @Html.Partial
because it goes through all the MVC steps of resolving views etc
When a section is optional you don't need to define it in the content page. Same as putting empty contents in aspx view engine.
???
Is there a 3rd party library has have more html helpers?
Yes, MVCContrib, WebHelpers, ...
Must read: ASP.NET MVC 3: Layouts and Sections with Razor
Upvotes: 5