public class Contact
{
public string Name { get; set; }
public string Email { get; set; }
public byte[] Photo { get; set; }
}
using System.Linq;
using System.Web.Mvc;
using Messenger.Views.Shared;
namespace Messenger.Controllers
public class HomeController : Controller
public ActionResult Index()
// On fabrique un modèle tout neuf
// et on affiche Index.aspx
IndexViewData model = new IndexViewData();
DataContext context = new DataContext();
model.Contacts = context.Contacts.ToList();
model.SelectedContact = model.Contacts[0];
ViewData.Model = model;
return View();
public ActionResult Read(string id)
// On construit le modèle en récuperant le
// contact selectionné.
// Puis on met à jour uniquement la liste
// des contacts et celui selectionné dans
// Index.aspx.
model.SelectedContact = model.Contacts.Where(c => c.Email == id).Single();
return View("Index");
using System;
public class ContactsController : Controller
public ActionResult New()
throw new NotImplementedException();
public ActionResult Add()
public class MessagesController : Controller
public ActionResult New(string id)
public ActionResult Send(string id)
using System.Collections.Generic;
namespace Messenger.Views.Shared
public class IndexViewData
public List<Contact> Contacts { get; set; }
public Contact SelectedContact { get; set; }
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Messenger.Views.Shared.Index"
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Messenger</title>
<link href="../../style.css" type="text/css" rel="Stylesheet" />
</head>
<body>
<div id="contacts">
<ul>
<% foreach (Contact contact in ViewData.Model.Contacts)
{ %>
<li><%= Html.ActionLink(contact.Name, "Read", "Home", new { id=contact.Email }) %></li>
<% } %>
</ul>
</div>
<div id="actions">
<%= Html.ActionLink("Add Contact", "New", "Contacts") %>
<%= Html.ActionLink("Send Msg", "New", "Messages", new { id = ViewData.Model.SelectedContact.Email })%>
<div id="panel">
<span>Name</span> <%= Html.AttributeEncode(ViewData.Model.SelectedContact.Name) %><br />
<span>Email</span> <%= Html.AttributeEncode(ViewData.Model.SelectedContact.Email) %><br />
</body>
</html>