@{
if (!WebSecurity.IsAuthenticated)
{
string currentUrl = Request.Url.ToString();
Response.Redirect("~/Account/Login?sender=" + currentUrl);
}
Layout = "~/Shared/Layouts/_Layout.cshtml";
Page.Title = "Checkout";
var address1 = "";
var address2 = "";
var town = "";
var region = "";
var postalCode = "";
var country = "";

// Redirect to Cart.cshtml if no cart exists in Session
if (Session["cart"] == null) {
        Response.Redirect("Cart");
    }

Cart cart = (Cart)Session["cart"];

if(cart.Items.Count() == 0)
{
Response.Redirect("Cart");
}

if (IsPost)
{
address1 = Request["address1"];
address2 = Request["address2"];
town = Request["town"];
region = Request["region"];
postalCode = Request["postalCode"];
country = Request["country"];

if (address1.IsEmpty()) {
        ModelState.AddError("address1", "*");
}
if (town.IsEmpty()) {
        ModelState.AddError("town", "*");
}
if (region.IsEmpty()) {
        ModelState.AddError("region", "*");
}
        if(ModelState.IsValid)
{

var UserID = WebSecurity.GetUserId(WebSecurity.CurrentUserName);
var db = Database.Open("MyeshopData");
var orderSql = "INSERT INTO Orders (UserId, OrderShipped, OrderTotal, " +
                "OrderDateTime, Address1, Address2, Town, " +
                "Region, PostalCode, Country) " +
                "VALUES ( @0, @1, @2, @3, @4, @5, @6, @7, @8, @9 )";
db.Execute(orderSql, UserID, false, cart.TotalValue, DateTime.Now, address1, address2, town, region, postalCode, country);
var orderId = db.GetLastInsertId();
foreach(var item in cart.Items) {
        orderSql = "INSERT INTO OrderItems (OrderID, ProductID, Size, Price) VALUES (" +
        "@0, @1, @2, @3)";
        db.Execute(orderSql, orderId, item.ProductID, item.Size, item.Price);
    }

//WebMail.Send(to: WebSecurity.CurrentUserName,
//subject: "myEshop Order Confirmation",
//body: "Many thanks for your order. Your items will be " +
//"dispatched shortly and should arrive within 3-5 working days."
//);


cart.Clear();
Response.Redirect("OrderComplete");
}
}
}
<h2>Checkout</h2>
<div id="checkout">
    <strong>Order Details</strong>
        <table id="cartTable">
        <tr>
        <th class="product">Product</th>
        <th class="size">Size</th>
        <th class="price">Price</th>
        </tr>
            @foreach (var item in cart.Items)
            {
            <tr>
            <td class="product">@Products.GetProductNameFromId(item.ProductID)</td>
            <td class="size">@item.Size</td>
            <td class="price">€@item.Price</td>
            </tr>
            }
    <tr class="cartTotal">
    <td colspan="2">&nbsp;</td>
    <td>Total: €@cart.TotalValue</td>
    </tr>
    </table>
    <strong>Delivery Details</strong>
    <form action="Checkout" method="post">
    <p>
    @Html.Label("Address Line 1: ", "address1")
    @Html.TextBox("address1", address1, new { maxlength = "100" } )
    @Html.ValidationMessage("address1")
    </p>
    <p>
    @Html.Label("Address Line 2:", address2)
    @Html.TextBox("address2", address2, new { maxlength = "100" } )
    </p>
    <p>
    @Html.Label("Town: ", "town")
    @Html.TextBox("town", town, new { maxlength = "50" } )
    @Html.ValidationMessage("town")
    </p>
    <p>
    @Html.Label("Region: ", "region")
    @Html.TextBox("region", region, new { maxlength = "50" } )
    @Html.ValidationMessage("region")
    </p>
    <p>
    @Html.Label("Postal Code: ", "postalCode")
    @Html.TextBox("postalCode", postalCode, new { maxlength = "20" } )
    </p>
    <p>
    @Html.Label("Country: ", "country")
    @Html.TextBox("country", country, new { maxlength = "50" } )
    </p>
    <p>
    <input type="submit" value="Place Order"/>
    </p>
    </form>
</div>