次の方法で共有


Creating an On Premise Database and MVC Web Application and Migrating to Windows Azure and SQL Azure-Step 3 of 10

Step 3The purpose of this post is to get our on-premise MVC application up and running. Let’s be clear. It will run “on-premise,” as we haven’t yet ported this app to run in the cloud.Be sure to review the two previous blogs posts before trying this one. The previous blogs show you how to create the projects and define the database.
hyperlink

https://blogs.msdn.com/b/brunoterkaly/archive/2010/06/12/creating-an-on-premise-database-and-mvc-web-application-and-migrating-to-windows-azure-and-sql-azure-step-1-of-10.aspx

hyperlink[4]

https://blogs.msdn.com/b/brunoterkaly/archive/2010/06/11/creating-an-on-premise-database-and-mvc-web-application-and-migrating-to-windows-azure-and-sql-azure.aspx

Modifying the controllerThe “controller” is the entry point to our application. When the end user types in a url, an MVC application will map it to the appropriate controller/action method combination.In other words, if the user types in https://localhost/home/index, that means the following:(1) HomeContoller.cs file will be accessed(2) The action method Index() will be called
snap0065[1]

Incorporating the Data Layer

An MVC application may be a collection of model/view/controller triplets, each responsible for a different UI element.
Controller The controller receives input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.
View The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes.
Model The model is used to manage information and notify observers when that information changes. The model is the domain-specific representation of the data upon which the application operates.
snap0066[1]

Database objects

ModelStocksContainerModelStocksContainer is an object that comes from the DataLayer. It was create when we created the DataLayer project. This object is needed so we can retrieve data.
snap0067[3]
 
snap0068[3]

Populating the model

Index MethodWe know the Index method gets called when the user issues the following url, https://localhost/home/index. That’s why we’ll issue the database query in the Index method.
snap0069[3]

Building the LINQ Query

Selecting data from the Stocks tableViewData.Model gets populated as a result of the query. ViewData.Model will be accessed by the View in a few moments.
snap0070[3]
The queryBe sure you code appears as follows.
snap0071[3]

 

Code Snippet

HomeController.cs
 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using DataLayer;namespace MvcOnPremise.Controllers{    [HandleError]    public class HomeController : Controller    {        ModelStocksContainer _db = new ModelStocksContainer();        public ActionResult Index()        {            ViewData.Model = (from m in _db.Stocks select m).ToList();            return View();        }        public ActionResult About()        {            return View();        }    }}

 

The View

Purpose of the viewThe view is used to display data. Views are automatically called and the developer doesn’t need to do anything special. Previously, we populated ViewData.Model with data. Our View will read this data and display it. Notice that Index.aspx appears as part of the Views folder.
snap0072[3]
 
snap0073[3]
Adding Stocks ListIn a moment, we will need some code to loop though ViewData.Model.
snap0074[3]

Looping through our data

ViewData.Model holds our dataWe will write a foreach loop to list the stock data.
snap0075[3]

 

snap0076[3]
snap0077[3]
snap0078[3]

The completed View (Index.aspx)

The finished foreach loopNotice that we are going to display the TickerSymbol and the Description

 

Code Snippet

Index.aspx
 <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">    Home Page</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    <h2>Stocks Data</h2>    <hr />    <%foreach (DataLayer.Stocks s in (IEnumerable<DataLayer.Stocks>)ViewData.Model)      {     %>     <b>Ticker Symbol =</b><%=s.TickerSymbol%><br />     <b>Description =</b><%=s.Description%><br /><hr />     <%} %></asp:Content>
snap0079[3]

Ready to Start Debugging

Our On-Premise MVC Application is doneWe are about to run the finished application.
snap0080[3]

The completed On-Premise Version

Cloud still not doneThe next post will start migrating this application to the cloud.
snap0081[3]

 

Upcoming – Converting this application to work with SQL Azure and Windows Azure

Future PostsSteps 4 to 10 will convert this application to run in the cloud – completely in the cloud.