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) + ""; } } }
http://host/HelloWorld
http://host/HelloWorld/Greet
http://host/HelloWorld/Welcome?name=Scott&numtimes=4
Add View dialog
Model
<%@ 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>
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; } } } }
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; } } } }
ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.