Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 _Layout.cshtml Controller

Can anyone help me with the subject? I'm using Razor view engine and I need to pass some data to _Layout. How can I do it?

like image 755
shumi Avatar asked Jan 14 '11 15:01

shumi


People also ask

What is _layout Cshtml in MVC?

The file MasterLayout. cshtml represents the layout of each page in the application. Right-click on the Shared folder in the Solution Explorer, then go to Add item and click View. Copy the following layout code.

What is the use of _ViewStart Cshtml in MVC?

The _ViewStart. cshtml page is a special view page containing the statement declaration to include the Layout page. Instead of declaring the Layout page in every view page, we can use the _ViewStart page. When a View Page Start is running, the “_ViewStart.

What is Cshtml in ASP NET MVC?

A file with . cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser. This server side coding is similar to the standard ASP.NET page enabling dynamic web content creation on the fly as the webpage is written to the browser.


1 Answers

As usual you start by creating a view model representing the data:

public class MyViewModel
{
    public string SomeData { get; set; }
}

then a controller which will fetch the data from somewhere:

public class MyDataController: Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            SomeData = "some data"
        };
        return PartialView(model);
    }
}

then a corresponding view (~/Views/MyData/Index.cshtml) to represent the data:

@{
    Layout = null;
}
<h2>@Model.SomeData</h2>

and finally inside your _Layout.cshtml include this data somewhere:

@Html.Action("index", "mydata")
like image 76
Darin Dimitrov Avatar answered Oct 15 '22 17:10

Darin Dimitrov