APRess ASP.NET MVC 4 (1)

Chapter 2 – Installing MVC 4
Anatomy of an ASP.NET Internet Application
App_Start – configurable files such as authentication, bundling, filtering, routing and WEBAPI.
Content – CSS and other assets in your project.
Controllers – Controllers
Filters – Action Filters are custom attributes that provide a declarative means to add a specific behavior to controller action methods. Simply the implementation of a logic.
Model – data and business logic classes.
Scripts – Javascripts
Views – UI Logic/HTML files.
Global.asax: Handle application and session level events
Packages.Config – Nuget

Chapter 3 – Build an application – “Have you seen me?”
(1) In VS 2012, create a MVC 4 Internet Application, debug, and go to login page. VS 2012 will create a LocalDB under /APP_Data.
image
(2) Right click the database file, and select “Include in Project”. Double click the database and VS will open the DB in Server Explorer.
image
(3) Right click “Table” and select New Query. You see a new query window. Cut and paste the SQL script in there (script from the book)
image
(4) Now you should have new tables created.
image
Chapter 4: Controllers
Controllers process incoming HTTP requests. Method in a controller is called action method because they return an object of type ActionResult.
(1) IN Global.asax., APplication_Start, it register all configuration files; including configuration, filter, routes, bundles and auth. All these classes are defined in app_start:
image
ViewResult (ActionResult)
image
FileResult
image
or this (this will prompt user to download)
image
(this will display the image in browser)
image
If user is accessing an unauthorized area, return this:
image
404 Not found error
Page 47
Chapter 5: Views
——————————–
@DateTime.Now
——————————–
@if (DateTime.Today.Year == 2103) {
<span>Current Year</span>
}
——————————–
<ul>
@foreach (var t in tweets) {
  <li>@t.content</li>
}
</ul>
——————————–
@{
var name=”Jose”;
var message = “Welcome “ + name;
}
———————————-
In Action method, right click anywhere in the method, and select “Add View” to add a view.
image
To return the default view for the current action method, simply do return View(); else, do return View(“NameOfView”)
image
image
image
In _Layout.cshtml, to display a section, use @RenderSection(“sectionname”, required: false)
@RenderBody(); renders the main contents.
Working with HTML Helpder Methods
Displays a link:
@Html.ActionLink(“Display text”, “ActionName”, ‘ControllerName”);
Render a partial view:
@Html.Partial(“MyViewName”);
image
_LoginPartial.cshtml is under /View/Shared/….
image
It displays different contents based on the Request.IsAuthenticated()
image
Here it either show log in links, or says “Hello, XXX”.
image

ViewData and ViewBag.
To pass information from an action method to a view, you basically have 3 options
1) ViewData
2) ViewBag (actually the same as ViewData, Except View bag has intellisense).Properties can be added to Viewbag dynamically.
3) strongly typed view with a view model object.

ViewData
image
In View, retrieve the values from ViewData[“….”]
image
ViewBag
image
Use ViewBag – it’s better
NOTICE!!!
In your action method, if you assign some data to ViewData/ViewBag, but then use RedirectToAction (HTTP 302 Redirect) to redirect to another action, then the data saved in ViewData/Viewbag will be lost!!!
In this case, you need to use “TempData” – it survives redirect. Saves the data in a “session” object. However, the data is discarded as soon as the view is rendered. Refreshing the page will cause an error.
Working with Strongly Typed Views
When you associate a view with a view model, the view becomes a strongly-typed view.
For example, this view model/class has three properties.
image
Now, this view is associated with that view model.
image
Inside your view, you simply refer to the model (m, a, z, x, or any letter or string to represent the model data passed from controller)
image
Chapter 6: Models
Page 69
Model –
Data Model – classes that interact with database. Normally created by EF. Closely match database tables.
Business Model – implement business functionalities/rules.
View Model – passed information from controller to views. Not exactly data model, but something in between data model and business model. You can define as many view models as needed and use them in different scenarios.
To add an EF data model, two ways –
(1) Using EF Reverse Engineer wizard
image

(2) Add a new item
image
To refresh your models (if you change anything in DB), do this:
image
image
Right click the entity (in design view) and you can change the Entity Set name to the correct form.
image
Page 81

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s