MVC Presentation
MVC Presentation
NET MVC
Introduction
The Model-View-Controller (MVC) is a software architecture
Drawbacks of ASP.NET
Display logic coupled with code, through code-behind files
Harder to unit test application logic, because of the coupled
code-behind files
ViewState and PostBack model
State management of controls leads to very large and often
ASP.NET Versions.
ASP.NET MVC 1.0 (Dec,2008)
ASP.NET MVC 2.0 Beta (Nov,2009)
Requirements
.NET Framework 3.5
Visual Studio 2008(service pack 1)
Visual Studio 2010(included)
Model=The
data
structure
View=Wha
t is
presented
to the
client
Model
View
Contro
ll
=Does er
t
proces he
sing
Controlle
r
MVC :MODEL-VIEW-CONTROLLER
HTTP
Request
Browser
Controller
Executio
n
Paramete
rs
Resulting
Data
Arrays
HTTP
Response
Resulting
Data
Arrays
GUI
Content
View
Model
ASP.NET
MVC
default
project
structure.
Default project has Global.asax
file which includes the routing
information
for
default
application.
The following are the default
folders that will come when new
MVC project is created.
Directory
/Controllers
requests
Purpose
Where you put Controller classes that handle URL
/Models
Where
manipulate data
/Views
you
put
classes
that
represent
and
/Scripts
(.js)
/Content Where you put CSS and image files, and other nondynamic/non-JavaScript content
/App_Data Where you store data files you want to read/write.
MVC Routing
Generally routing information is given in Global.asax file.
namespace MvcApplication1
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with
parameters
new { controller = "Home", action = "Index", id = "" }
//Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
Controller
MVC controllers are responsible for responding to requests
made against an ASP.NET MVC website. Each browser
request is mapped to a particular controller.
A set of action methods that share a common Controller
context.
An Action:
Receives the request parameters.
Creates any required model (user, etc..).
Calls services or repositories to execute data or get the
models to be displayed.
Selects the view and sends the model to it.
Example:
http://localhost:49450/Home/Index
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC!;
return View();
}
public ActionResult About()
{
ViewData["Title"] = "About Page;
return View();
}
}
}
View
Just renderer
No logic should go there
Code Behind exists but not recommended
No ViewState, No Server Controls
Takes the view data from the controller.
Can be typed.(strongly typed i.e. model is included)
View Example
Index.aspx.
<%@ Page Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="aboutContent"
ContentPlaceHolderID="MainContent" runat="server">
<h2><% : ViewData[Message"] %></h2>
<p>
Put content here.
</p>
</asp:Content>
Model
An MVC model contains all of our application logic that is not
contained in a view or a controller.
The model should contain all of our application business logic,
validation logic, and database access logic.
For example, if you are using the Microsoft Entity Framework
to access your database, then you would create your Entity
Framework classes (your .edmx file) in the Models folder.
A view should contain only logic related to generating the user
interface. A controller should only contain the bare minimum of
logic required to return the right view or redirect the user to
another action (flow control). Everything else should be
contained in the model.
Example
namespace MvcApplication1.Models
{
public class HomeModel
{
}
public class Users
{
// Database connections will go here..
public int UserId { set; get; }
public string Name { set; get; }
public string Address { set; get; }
// Database operations will go here..
}
}
Thank Q
Any
Questions??