0% found this document useful (1 vote)
3K views4 pages

MVC Fundamentals Cheat Sheet PDF

This document discusses key concepts in ASP.NET MVC including action results that determine how an action method responds, sources for action parameters, convention-based and attribute routes for mapping URLs to actions, passing data to views through models rather than ViewData/ViewBag, logic and looping in Razor views, and rendering partial views. It provides examples of common patterns used in ASP.NET MVC applications.

Uploaded by

shredernie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views4 pages

MVC Fundamentals Cheat Sheet PDF

This document discusses key concepts in ASP.NET MVC including action results that determine how an action method responds, sources for action parameters, convention-based and attribute routes for mapping URLs to actions, passing data to views through models rather than ViewData/ViewBag, logic and looping in Razor views, and rendering partial views. It provides examples of common patterns used in ASP.NET MVC applications.

Uploaded by

shredernie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ASP.

NET MVC Fundamentals By: Mosh Hamedani

Action Results
Type Helper Method

ViewResult View()

PartialViewResult PartialView()

ContentResult Content()

RedirectResult Redirect()

RedirectToRouteResult RedirectToAction()

JsonResult Json()

FileResult File()

HttpNotFoundResult HttpNotFound()

EmptyResult

Action Parameters
Sources
Embedded in the URL: /movies/edit/1
In the query string: /movies/edit?id=1
In the form data

1
ASP.NET MVC Fundamentals By: Mosh Hamedani

Convention-based Routes
routes.MapRoute(
MoviesByReleaseDate,
movies/released/{year}/{month},
new {
controller = Movies,
action = MoviesReleaseByDate
},
new {
year = @\d{4}, month = @\d{2}
} isFavorite = false;
}

Attribute Routes
[Route(movies/released/{year}/{month})
public ActionResult MoviesByReleaseDate(int year, int month)
{
}

To apply a constraint use a colon:

month:regex(\\d{2}):range(1, 12)

2
ASP.NET MVC Fundamentals By: Mosh Hamedani

Passing Data to Views


Avoid using ViewData and ViewBag because they are fragile. Plus, you have to do extra
casting, which makes your code ugly. Pass a model (or a view model) directly to a view:

return View(movie);

Razor Views

@if ()
{
// C# code or HTML
}

@foreach ()
{
}

Render a class (or any attributes) conditionally:

@{
var className = Model.Customers.Count > 5 ? popular : null;
}
<h2 class=@className></h2>

3
ASP.NET MVC Fundamentals By: Mosh Hamedani

Partial Views
To render:

@Html.Partial(_NavBar)

You might also like