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

Step 3
The 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.



Modifying the controller
The “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 http://localhost/home/index, that means the following:
(1) HomeContoller.cs file will be accessed
(2) The action method Index() will be called


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.


Database objects

ModelStocksContainer 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.




Populating the model

Index Method
We know the Index method gets called when the user issues the following url, http://localhost/home/index. That’s why we’ll issue the database query in the Index method.


Building the LINQ Query

Selecting data from the Stocks table
ViewData.Model gets populated as a result of the query. ViewData.Model will be accessed by the View in a few moments.


The query
Be sure you code appears as follows.



Code Snippet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DataLayer;

namespace MvcOnPremise.Controllers
    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 view
The 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.




Adding Stocks List
In a moment, we will need some code to loop though ViewData.Model.


Looping through our data

ViewData.Model holds our data
We will write a foreach loop to list the stock data.






The completed View (Index.aspx)

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


Code Snippet

<%@ 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 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 />
     <%} %>


Ready to Start Debugging

Our On-Premise MVC Application is done
We are about to run the finished application.


The completed On-Premise Version

Cloud still not done
The next post will start migrating this application to the cloud.



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

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

Comments (0)

Skip to main content