MVC Basics - ASP.NET MVC

This article was adapted from here: Intro to ASP.NET MVC.

Overview

MVC is an abbreviation for Model-View-Controller.


Controllers

Whatever is returned by a controller's action method is rendered as HTML.

using System.Web.Mvc; 
   
namespace Movies.Controllers 
{ 
    public class HelloWorldController : Controller 
    { 
        public string Index() 
        { 
            return "This is my default action...";       
        } 
          
        public string Greet() 
        {   
            return "This is the Greet action method..."; 
        }  
        public string Welcome(string name, int numTimes = 1) 
        { 
           string message = "Hello " + name + ", NumTimes is: " + numTimes; 
           return "" + Server.HtmlEncode(message) + ""; 
        }
    } 
}


Best practice is NOT to render HTML directly (like above), but rather to call a View template. A View template is added via the context menu option.

Image

Views

Simple

A simple view can be created as follows, and ends up being an ASPX file in the Views folder

Add View dialog

Add View dialog


Strongly-Typed

To create a strongly-typed view,

  1. create your ViewModel class nested within your controller class (See below for ViewModels)
  2. successfully build your code
  3. as before, use the context menu for the controller method you want to add a View for
  4. You can then refer to members of your ViewModel via the Model object.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<Movies.Controllers.HelloWorldController+WelcomeViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Welcome
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Welcome</h2>
    <% for (int i = 0; i < Model.NumTimes; i++)
       { %>
    <h3><%: Model.Message%></h3>
    <%} %>

</asp:Content>

ViewModels

A ViewModel is an object that represents what a View template requires to render an HTML response back to a client. It is typically created and passed by a Controller class to a View template, and should only contain the data that the View template requires - and no more.

Example

using System.Web.Mvc; 
  
namespace Movies.Controllers 
{ 
    public class HelloWorldController : Controller 
    { 
        public ActionResult Index() 
        { 
            return View(); 
        } 
  
        public ActionResult Welcome(string name, int numTimes = 1) 
        { 
            var viewModel = new WelcomeViewModel 
            { 
                Message = "Hello " + name, 
                NumTimes = numTimes 
            }; 
  
            return View(viewModel); 
        } 
  
        public class WelcomeViewModel
        { 
            public string Message { get; set; } 
            public int NumTimes { get; set; } 
        }
    } 
}

Validation

Creating the following partial class will automatically add server-side validation code to

using System.ComponentModel.DataAnnotations; 
  
namespace Movies.Models 
{ 
    [MetadataType(typeof(MovieMetadata))] 
    public partial class Movie 
    { 
        class MovieMetadata 
        { 
            [Required(ErrorMessage="Titles are required")] 
            public string Title { get; set; } 
  
            [Required(ErrorMessage="The Price is required.")] 
            [Range(5,100,ErrorMessage ="Movies cost between $5 and $100.")] 
            public decimal Price { get; set; } 
        } 
    } 
}