MVC study

 

  • default generated folders

Folder

Purpose

/Controllers

Controllers respond to input from the browser, decide what to do with it, and return response to the user.

/Views

Views hold our UI templates

/Models

Models hold and manipulate data

/Content

This folder holds our images, CSS, and any other static content

/Scripts

This folder holds our JavaScript files

/App_Data

This folder holds our database files

based on folder naming convertion, for example, controllers look for views in Views folder

advantages: reduce typing codes and make it easier to understand

 

  • controllers

traditional sites: mapping incoming url/ request to files, e.g. /index.aspx

MVC sites: mapping url to methods of classes, these classes are called controllers, these methods are called controller actions

functions:

  • processing request
  • handle input
  • deal with data
  • determining response - HTML/redirect/download file...

notes:

  • Default home page naming convertion: HomeController
  • get parametres from methods' params
  • HttpUtility.HTMLEncode to prevent users from injecting js into our view

 

 

 

  • ViewModel

what viewModel to do: encapsulate the data that controllers want to pass to views

build project before view knows new created viewModel class

 

 

  • Views

what views to do: using template files to more easily customize the HTML contents back to browsers

 

to use view-template, return ActionResult and View();

view's folder location matchs controller's name

view's file name matchs method's name

 

<% %> "code nuggets"

<%: %> result will be output to the page instead <%= %>

 

 

  • Model Class

manage data and domain logic, unlike viewModel, which was built just for passing data from controller to view

 

 

  • HTML helper

ActionLink create <a> and ensure the URL paths

example:

<%: Html.ActionLink("Go to the Store Index","Index")%>
two params: link text and same controller's method

<%: Html.ActionLink(genreName, "Browse", new { genre = genreName })%>

three params: link text, same controller's method and input

 

Html.EditorForModel()  will first look for the template, otherwise use default template

<%: Html.EditorFor(model => model.Album, 

            new { Artists = Model.Artists, Genres = Model.Genres}) %>

 allows us to specify a specific Editor type 

 

<%: Html.DropDownList("ArtistId", new SelectList(ViewData["Artists"] as IEnumerable, 
    "ArtistId", "Name", Model.ArtistId))%>

 

  • The name of the form field (ArtistId)
  • The list of values for the dropdown, passed as a SelectList
  • The Data Value field which should be posted back with the form
  • The Data Text field which should be displayed in the dropdown list
  • The Selected Value which is used to set the dropdown list value when the form is displayed

 

  • Entity Data Model

Create EDM via Entity Framework, EF - an ORM(object relational mapping) Data API to query and update data stored in DB

ADO.Net Entity data model represents one Database

 

 

  • Editor Template

Must in folder: /Views/Shared/EditorTemplates

Add view as a partial view(.ascx)

 

 

  • Cascade delete

delete one record in table a also delete records in table b connected with table a

set "End1 OnDelete"=Cascade in the relation between table a and table b

 

 

posted on 2010-11-23 12:38  Joe Hou  阅读(227)  评论(0)    收藏  举报