Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Chapter 3

  • Loading branch information...
commit b943aac4f60773df6959af54f58f92f6c34117d4 1 parent 8a9e7ed
@jchadwick jchadwick authored
View
BIN  Ebuy.Website/App_Data/Ebuy.Website.Models.EbuyDataContext.mdf
Binary file not shown
View
BIN  Ebuy.Website/App_Data/Ebuy.Website.Models.EbuyDataContext_log.ldf
Binary file not shown
View
23 Ebuy.Website/Controllers/AuctionsController.cs
@@ -1,8 +1,6 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Web;
using System.Web.Mvc;
+using Ebuy.Website.Models;
namespace Ebuy.Website.Controllers
{
@@ -28,15 +26,16 @@ public ActionResult Details(long id = 0)
Description = "This is a brand new version 2.0 Widget!",
StartPrice = 1.00m,
CurrentPrice = 13.40m,
- StartTime = DateTime.Parse("6-15-2012 12:34 PM"),
EndTime = DateTime.Parse("6-23-2012 12:34 PM"),
};
return View(auction);
}
+
//
// GET: /Auctions/Create
+ [HttpGet]
public ActionResult Create()
{
return View();
@@ -46,18 +45,18 @@ public ActionResult Create()
// POST: /Auctions/Create
[HttpPost]
- public ActionResult Create(FormCollection collection)
+ public ActionResult Create(Auction auction)
{
- try
+ if (ModelState.IsValid)
{
- // TODO: Add insert logic here
+ var db = new EbuyDataContext();
+ db.Auctions.Add(auction);
+ db.SaveChanges();
- return RedirectToAction("Index");
- }
- catch
- {
- return View();
+ return RedirectToAction("Details", new { id = auction.Id });
}
+
+ return View(auction);
}
//
View
1  Ebuy.Website/Ebuy.Website.csproj
@@ -284,6 +284,7 @@
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\Web.config" />
<Content Include="Views\Auctions\Details.cshtml" />
+ <Content Include="Views\Auctions\Create.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
View
25 Ebuy.Website/Models/Auction.cs
@@ -1,15 +1,38 @@
using System;
+using System.ComponentModel.DataAnnotations;
namespace Ebuy.Website.Models
{
+ using System.Data.Entity;
+
+ public class EbuyDataContext : DbContext
+ {
+ public DbSet<Auction> Auctions { get; set; }
+
+ public EbuyDataContext()
+ {
+ Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EbuyDataContext>());
+ }
+ }
+
public class Auction
{
public long Id { get; set; }
+
+ [Required]
+ [StringLength(50,
+ ErrorMessage = "Title cannot be longer than 50 characters")]
public string Title { get; set; }
+
+ [Required]
public string Description { get; set; }
+
+ [Range(1, 10000,
+ ErrorMessage = "The auction's starting price must be at least 1")]
public decimal StartPrice { get; set; }
+
public decimal CurrentPrice { get; set; }
- public DateTime StartTime { get; set; }
+
public DateTime EndTime { get; set; }
}
}
View
32 Ebuy.Website/Views/Auctions/Create.cshtml
@@ -0,0 +1,32 @@
+@model Ebuy.Website.Models.Auction
+
+<h2>Create Auction</h2>
+
+@using (Html.BeginForm())
+{
+ @Html.ValidationSummary()
+
+ <p>
+ @Html.LabelFor(model => model.Title)
+ @Html.EditorFor(model => model.Title)
+ @Html.ValidationMessageFor(model => model.Title, "*")
+ </p>
+ <p>
+ @Html.LabelFor(model => model.Description)
+ @Html.EditorFor(model => model.Description)
+ @Html.ValidationMessageFor(model => model.Description, "*")
+ </p>
+ <p>
+ @Html.LabelFor(model => model.StartPrice)
+ @Html.EditorFor(model => model.StartPrice)
+ @Html.ValidationMessageFor(model => model.StartPrice)
+ </p>
+ <p>
+ @Html.LabelFor(model => model.EndTime)
+ @Html.EditorFor(model => model.EndTime)
+ @Html.ValidationMessageFor(model => model.EndTime)
+ </p>
+ <p>
+ <input type="submit" value="Create" />
+ </p>
+}
View
7 Ebuy.Website/Views/Auctions/Details.cshtml
@@ -38,13 +38,6 @@
</div>
<div class="display-label">
- @Html.DisplayNameFor(model => model.StartTime)
- </div>
- <div class="display-field">
- @Html.DisplayFor(model => model.StartTime)
- </div>
-
- <div class="display-label">
@Html.DisplayNameFor(model => model.EndTime)
</div>
<div class="display-field">
Please sign in to comment.
Something went wrong with that request. Please try again.