chobo2
chobo2

Reputation: 85765

Questions about the Razor engine in asp.net mvc 3

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.

  1. 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.

  2. 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?

  3. 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?

  4. Can you have _ViewStart.cshtml in the shared folder?

  5. 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?

  6. Is @RenderPage used for loading partial views or is it used for something different?

  7. What is the difference from using @RenderSection Optional and just not putting anything between the content tags if you were using the aspx engine?

  8. What happens if you want the stuff in the @RenderSection to stored in a partial view. Can you still make it optional?

not specific to asp.net mvc 3

  1. Is there a 3rd party library has have more html helpers?

Thanks

Upvotes: 2

Views: 898

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

  1. 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.

  2. It is obsolete and it won't compile in ASP.NET MVC 3 RTM

  3. 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.

  4. No
    Yes, but it will only get executed if the view being executed is located in the Shared folder.

  5. Yes

  6. @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

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

  8. ???

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

Related Questions