How Can I Have View-Specific contents Using Asp.Net MVC 3 and Razor?

The equivalent of content placeholders in Razor are sections.

In your _Layout.cshtml:

<head>
@RenderSection("Styles", required: false)
</head>

Then in your content page:

@section Styles {
    <link href="https://stackoverflow.com/questions/4739907/@Url.Content("~/Content/StandardSize.css")" />
}

An alternative solution would be to put your styles into ViewBag/ViewData:

In your _Layout.cshtml:

<head>
    @foreach(string style in ViewBag.Styles ?? new string[0]) {
        <link href="https://stackoverflow.com/questions/4739907/@Url.Content(style)" />
    }
</head>

And in your content page:

@{
    ViewBag.Styles = new[] { "~/Content/StandardSize.css" };
}

This works because the view page gets executed before the layout.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)