MVC Fundamentals Exercise Hints
MVC Fundamentals Exercise Hints
MVC Fundamentals Exercise Hints
Exercise Hints
Creating Links
There are a few different ways to create links in ASP.NET MVC apps. The
simplest way is to use raw HTML:
If the target action requires a parameter, you can use an anonymous object to pass
parameter values:
/movies/index/1
But for a reason only known to programmers at Microsoft, this method doesnt generate
this link, unless you pass another argument to the ActionLink. This argument can be null
or an anonymous object to render any additional HTML attributes:
1
ASP.NET MVC Fundamentals By: Mosh Hamedani
So, ActionLink or raw HTML, which approach is better? ActionLink queries the routing
engine for the URL associated with the given action. If you have a custom URL
associated with an action, and change that URL in the future, ActionLink will pick the
latest URL, so you don't need to make any changes. But with raw HTML, you need to
update your links when URLs are changed.
Having said, changing URLs is something you should avoid because these URLs are the
public contract of your app and can be referenced by other apps or bookmarked by
users. If you change them, all these bookmarks and external references will be broken.
So, at the end, whether you prefer to use raw HTML or @Html.ActionLink() is your
personal choice.
HTML Tables
In the demo I showed you, I used a few CSS classes on my HTML table to give it
the look and feel you saw in the video. These classes are part of Bootstrap, a
front-end framework for building modern, responsive applications.
ViewResult View()
2
ASP.NET MVC Fundamentals By: Mosh Hamedani
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
3
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)
{
}
month:regex(\\d{2}):range(1, 12)
4
ASP.NET MVC Fundamentals By: Mosh Hamedani
return View(movie);
Razor Views
@if
()
{
//
C#
code
or
HTML
}
@foreach
()
{
}
@{
var
className
=
Model.Customers.Count
>
5
?
popular
:
null;
}
<h2
class=@className></h2>
5
ASP.NET MVC Fundamentals By: Mosh Hamedani
Partial Views
To render:
@Html.Partial(_NavBar)