Professional Documents
Culture Documents
TP Photos
TP Photos
TP Photos
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
if (photo == null)
{
return RedirectToAction(
"DisplayError",
new { message = "No photo for that id" });
}
return View(photo);
}
return View();
}
if (photo != null)
{
return new FileContentResult(
photo.PhotoFile, photo.ImageMimeType);
}
else
return null;
}
if (photo == null)
{
return RedirectToAction(
"DisplayError",
new { message = "No photo for that title" });
}
//On va envoyer la vue Détails avec le model photo
return View("Details", photo);
}
[HttpPost]
public ActionResult Create(Photo photo,
HttpPostedFileBase image)
{
photo.CreatedDate = DateTime.Now;
if(!ModelState.IsValid)
{
//Le model state encpsule l'etat de validation du model
// foreach( var item in ModelState.Keys)
// {
// Response.Write(item +" "+ModelState[item].Errors[0].ErrorMessage+"<br/>");
return View("Create", photo);
// }
}
else
{
if(image!=null)
{
photo.ImageMimeType = image.ContentType;
photo.PhotoFile = new byte[image.ContentLength];
image.InputStream.Read(photo.PhotoFile,0, image.ContentLength);
}
db.Photos.Add(photo);
db.SaveChanges();
}
return RedirectToAction("Index");
}
}
}
Models
namespace WebApplicationPhotos.Models
{
public class Comment
{
public int CommentId { get; set; }
public int PhotoId { get; set; }
[Required]
[StringLength(250)]
public string UserName { get; set; }
[DataType(DataType.MultilineText)]
public string Subject { get; set; }
public string Body { get; set; }
namespace WebApplicationPhotos.Models
{
//Initialisateur de base de données
public class PhotDBInitializer :
DropCreateDatabaseAlways<PhotosContext>
{
public byte[] getFileBytes(string path)
{
//Ouvrire le fichier path en mode read
FileStream
fileOnDisk =
new
FileStream(HttpRuntime.AppDomainAppPath + path,
FileMode.Open);
byte[] fileBytes;
};
};
}
}
namespace WebApplicationPhotos.Models
{
public class Photo
{
[Required]
[DisplayName("Id")]
public int PhotoId { get; set; }
[Required]
public string Title { get; set; }
[DisplayName("Photo")]
public byte[] PhotoFile { get; set;}
public string ImageMimeType { get; set; }
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[DataType(DataType.DateTime)]
[DisplayName("Created date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yy}")]
public DateTime CreatedDate { get; set; }
public string UserName { get; set; }
namespace WebApplicationPhotos.Models
{
public class PhotosContext : DbContext
{
public PhotosContext() :
base()
{
}
public DbSet<Photo> Photos { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
Views
@model WebApplicationPhotos.Models.Photo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
@using (Html.BeginForm("Create","Photo",FormMethod.Post ,new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Photo</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhotoFile, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="image" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
View Details
@model WebApplicationPhotos.Models.Photo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Details</title>
</head>
<body>
<div>
<h4>Photo</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.PhotoFile)
</dt>
<dd>
@if (Model.PhotoFile != null)
{
<img src='@Url.Action("GetImage","Photo",new { id= Model.PhotoId})'
}
</dd>
<dt>
@Html.DisplayNameFor(model => model.ImageMimeType)
</dt>
<dd>
@Html.DisplayFor(model => model.ImageMimeType)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd>
@Html.DisplayFor(model => model.Description)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CreatedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CreatedDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.UserName)
</dt>
<dd>
@Html.DisplayFor(model => model.UserName)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.PhotoId }) |
@Html.ActionLink("Back to List", "Index")
</p>
</body>
</html>
View DisplayError
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>DisplayError</title>
</head>
<body>
<div>
<h1> @ViewData["message"]</h1>
</div>
</body>
</html>
View index
@model IEnumerable<WebApplicationPhotos.Models.Photo>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table border="1" class="table">
<tr>
<th>
</th>
<th>
@Html.DisplayNameFor(model => model.PhotoId)
</th>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.ImageMimeType)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
photo
</th>
</tr>
}
</td>
</tr>
}
</table>
<p>
@Html.ActionLink("Create New", "Create")
</p>
</body>
</html>
namespace WebApplicationMVC1.Controllers
{
public class HomeController : Controller
{
//Represente le model - la base de données
tachesDataContext db =
new tachesDataContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=tachesDB;Integrated Security=True");
// GET: Home
public ActionResult Index()
{
//Ramener l'ensemble des taches
/* var query = from tache in db.Taches
select tache;*/
//Un traitement
//la liste des taches constituent le modele de la vue
return View(db.Taches.ToList());
}
Taches tache=query.FirstOrDefault();
if (tache != null)
tache.complete = true;
db.SubmitChanges();
//Un traitement
return RedirectToAction("Index");
}
}
}