ASP.NET Core MVC View Types

ASP.NET Core MVC View Types

Understanding different view types and Action Results used in ASP.NET Core MVC applications.

View Type Usage Example
Normal View Displays a page and returns HTML to the browser.
public IActionResult Index()
{
    return View();
}
Strongly Typed View Passes a Model object from Controller to View.
public IActionResult StudentEntry()
{
    return View(new StudentModel());
}
View:
@model StudentModel

@Model.Name
Partial View Reusable UI components such as Header, Footer, Menu and Grid.
return PartialView("_StudentGrid");
View:
@await Html.PartialAsync("_StudentGrid")
ViewBag Pass temporary data from Controller to View without creating a model.
ViewBag.CompanyName = "ABC ERP";

return View();
View:
@ViewBag.CompanyName
ViewData Similar to ViewBag but uses a Dictionary object.
ViewData["Title"] = "Student Entry";
View:
@ViewData["Title"]
TempData Pass data between requests. Commonly used after Save/Delete operations.
TempData["Message"] =
"Record Saved Successfully";
View:
@TempData["Message"]
JSON Result Used in AJAX requests. Returns JSON instead of HTML.
return Json(new
{
    success = true,
    message = "Saved"
});
Content Result Returns plain text content.
return Content("Hello World");
File Result Used to download PDF, Excel, Images and other files.
return File(
    fileBytes,
    "application/pdf",
    "Report.pdf");
Redirect Result Redirect user to another Action.
return RedirectToAction("Index");

Recommended Usage

Scenario Recommended View Type
Data Entry Form Strongly Typed View
Grid Refresh Using AJAX Partial View + JSON Result
Save Record JSON Result
Download PDF File Result
Success Message After Redirect TempData
Reusable Menu/Header/Footer Partial View

MVC View Folder Structure

Views
│
├── Home
│   └── Index.cshtml
│
├── Student
│   ├── StudentEntry.cshtml
│   └── _StudentGrid.cshtml
│
├── Shared
│   ├── _Layout.cshtml
│   ├── _Header.cshtml
│   └── _Footer.cshtml
│
└── ViewImports.cshtml

Best Practice: Use Strongly Typed Views for data entry screens. Use Partial Views for reusable UI components. Use JSON Results for AJAX operations. Avoid excessive use of ViewBag and ViewData in large projects.