Skip to content

Commit

Permalink
Chapter 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jchadwick committed Oct 1, 2012
1 parent 8a9e7ed commit b943aac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 20 deletions.
Binary file not shown.
Binary file not shown.
23 changes: 11 additions & 12 deletions 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
{
Expand All @@ -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();
Expand All @@ -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);
}

//
Expand Down
1 change: 1 addition & 0 deletions Ebuy.Website/Ebuy.Website.csproj
Expand Up @@ -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\" />
Expand Down
25 changes: 24 additions & 1 deletion 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; }
}
}
32 changes: 32 additions & 0 deletions 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>
}
7 changes: 0 additions & 7 deletions Ebuy.Website/Views/Auctions/Details.cshtml
Expand Up @@ -37,13 +37,6 @@
@Html.DisplayFor(model => model.CurrentPrice)
</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>
Expand Down

0 comments on commit b943aac

Please sign in to comment.