Layout
Layout
NET MVC
A Layout View in ASP.NET MVC is a reusable template that defines a consistent structure for
web pages, such as headers, footers, navigation menus, and more. It is similar to a "Master Page"
in Web Forms.
Dynamic
<asp:Content> for individual pages Razor code for embedding dynamic content
Content
Usage Used in Web Forms for consistent design. Used in MVC for consistent page layouts.
Place the layout view in the Views/Shared folder for global access.
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title - My ASP.NET MVC App</title>
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
<header>
<h1>My ASP.NET MVC Application</h1>
<nav>
<a href="@Url.Action("Index", "Home")">Home</a> |
<a href="@Url.Action("About", "Home")">About</a>
</nav>
</header>
<div class="container">
@RenderBody()
</div>
<footer>
<p>© @DateTime.Now.Year - My MVC App</p>
</footer>
</body>
</html>
In individual views (e.g., Index.cshtml), reference the layout view using the @layout
directive.
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
You can define optional sections in the layout view using @RenderSection.
<div class="container">
@RenderBody()
@section Scripts {
<script>
console.log("Custom script for this view.");
</script>
}
// Models/User.cs
namespace YourNamespace.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
}
// Controllers/HomeController.cs
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
public class HomeController : Controller
{
public ActionResult UserDetails()
{
// Creating a sample User object
var user = new User
{
Id = 1,
Name = "John Doe",
Email = "[email protected]"
};
@model YourNamespace.Models.User
<!DOCTYPE html>
<html>
<head>
<title>User Details</title>
</head>
<body>
<h1>User Details</h1>
<p><strong>ID:</strong> @Model.Id</p>
<p><strong>Name:</strong> @Model.Name</p>
<p><strong>Email:</strong> @Model.Email</p>
</body>
</html>
How It Works
1. Binding the View to the Model: The @model directive specifies the model type that the view
expects.
2. Passing the Model: The controller creates the User object and passes it to the view using
return View(model);.
3. Rendering the Model Properties: The view accesses the properties of the User model (e.g.,
@Model.Name, @Model.Email) for rendering.
Type Safety: Errors in accessing model properties are caught at compile time.
IntelliSense Support: Provides suggestions and auto-completion for model properties.
Cleaner Code: Removes the need for magic strings like ViewBag or ViewData.
A Partial View in ASP.NET MVC is a reusable component for rendering small parts of a view.
It is ideal for creating modular and reusable sections, such as headers, footers, menus, or custom
widgets.
Partial views are typically stored in the Views/Shared folder for reuse across multiple
controllers or in a specific view folder.
File: _UserDetails.cshtml
@model YourNamespace.Models.User
<div class="user-details">
<h3>@Model.Name</h3>
<p>Email: @Model.Email</p>
</div>
2. Controller
File: HomeController.cs
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
// Sample data for the main view
var user = new User
{
Id = 1,
Name = "John Doe",
Email = "[email protected]"
};
3. Main View
Render the partial view in the main view using Html.Partial or Html.RenderPartial.
File: Index.cshtml
@model YourNamespace.Models.User
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Welcome to the Main View</h1>
csharp
Copy code
ViewData["Message"] = "Hello from ViewData!";
Partial View:
html
Copy code
<p>@ViewData["Message"]</p>
If the partial view has logic tied to a controller action, use Html.Action.
Controller:
public ActionResult UserDetails()
{
var user = new User
{
Id = 1,
Name = "Jane Doe",
Email = "[email protected]"
};
Main View:
@Html.Action("UserDetails", "Home")
Slightly slower for large data because it More efficient for large data because it
Performance
processes the output as a string first. avoids the overhead of returning a string.
Code Examples
1. Using Html.Partial
html
Copy code
@{
Html.RenderPartial("_UserDetails", Model);
}
Html.Partial:
o Use when you want more flexibility, like storing the result in a variable for further
manipulation.
o Suitable for simple or smaller content rendering.
Html.RenderPartial:
o Use when performance is a concern, especially for large-scale content.
o Suitable for rendering directly without additional processing.
A Partial View in ASP.NET MVC is a reusable component designed to render smaller portions
of a web page. It is useful for creating modular, maintainable, and reusable sections of a user
interface, such as headers, footers, menus, or widgets.
1. Add a new partial view file to the Views/Shared folder (recommended for global use) or a
specific view folder.
2. Name the file with a preceding underscore, e.g., _UserDetails.cshtml.
Example: _UserDetails.cshtml
@model YourNamespace.Models.User
<div class="user-details">
<h3>@Model.Name</h3>
<p>Email: @Model.Email</p>
</div>
Controller: HomeController.cs
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
Id = 1,
Name = "John Doe",
Email = "[email protected]"
};
return View(user);
}
Step 3: Render the Partial View in the Main View
Using Html.Partial
View: Index.cshtml
@model YourNamespace.Models.User
<h1>Main View</h1>
@Html.Partial("_UserDetails", Model)
Using Html.RenderPartial
@model YourNamespace.Models.User
<h1>Main View</h1>
@{ Html.RenderPartial("_UserDetails", Model); }
Using Html.Action
Controller Action
View
@Html.Action("GetUserDetails", "Home")
1. Modular Design: Keeps code organized by breaking views into smaller components.
2. Reusability: Common UI elements can be reused across multiple views.
3. Performance: Improves page load times by rendering only necessary sections.
4. Dynamic Content: Supports partial updates via AJAX.
Used when the result needs to be Used to render directly to the view without
Usage
stored in a variable or rendered inline. storing or modifying the output.
1. Create a Model
// Models/User.cs
namespace YourNamespace.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
}
The partial view file should include the @model directive to bind it to the User model.
File: _UserDetails.cshtml
@model YourNamespace.Models.User
<div class="user-details">
<h3>User Information</h3>
<p><strong>ID:</strong> @Model.Id</p>
<p><strong>Name:</strong> @Model.Name</p>
<p><strong>Email:</strong> @Model.Email</p>
</div>
3. Controller
// Controllers/HomeController.cs
using System.Web.Mvc;
using YourNamespace.Models;
namespace YourNamespace.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
var user = new User
{
Id = 1,
Name = "John Doe",
Email = "[email protected]"
};
File: Index.cshtml
@model YourNamespace.Models.User
<!DOCTYPE html>
<html>
<head>
<title>Main View</title>
</head>
<body>
<h1>Main View</h1>
1. Type Safety: Reduces runtime errors by validating the model at compile time.
2. IntelliSense: Provides auto-completion for model properties in the view.
3. Modular Design: Simplifies development by breaking down the UI into reusable components.
4. Maintainability: Easier to debug and update due to clear data bindings.
This approach is highly recommended for complex applications to ensure maintainable and
error-free code.