title: How to code an auction site with ASP.Net/ MVC 5 / EF
body: <p>I'm relatively new to ASP.NET so please bear with me.</p>
  
  <p>I'm trying to code a straightforward auction site for a charity, using MVC 5 and Entity Framework with Code-First.</p>
  
  <p>I have created an Item model and controller. The Item model holds fields like the title, description, starting bid, current bid, number of bids, high bidder, etc.</p>
  
  <pre><code>public class Item
  {
      public int ID { get; set; }
      public string Code { get; set; }
      public string Title { get; set; }
      public string Description { get; set; }
      [DisplayName("Starting Bid")] public int StartingBid { get; set; }
      public int Increment {get; set;}
      public int Bids { get; set; }
      [DisplayName("Current Bid")] public int CurrentBid { get; set; }
      public string HighBidder { get; set; }
  
      public int CurrentOrStartingBid
      { // The price that is displayed next to an item
          get
          {
              return Bids &gt; 0 ? CurrentBid : StartingBid;
          }
      }
      public int NextBid
      { // The minimum amount that is valid for a new bid
          get
          {
              return Bids &gt; 0 ? CurrentBid + Increment : StartingBid;
          }
      }
  }
  </code></pre>
  
  <p>(What I have tried to do in the code above is to add these properties CurrentOrStartingBid and NextBid which are not intended to be part of the database record, they are just derivative properties rather than columns in the DB. So hopefully having these as read-only properties will do that...)</p>
  
  <p>I am now making a view for the item detail. This shows the item description, and also features form controls for placing a bid, similar to what you would see on eBay. My question is about how to wire up the logic for the Bid button correctly.</p>
  
  <p>I figure that in the view I should use an HTML form with a submit button for the bidding. This does an HTTP post when the button is hit, and allows me to write a method in the Item controller that gets called at that time.</p>
  
  <p>So my Razor code in the view currently looks like this:</p>
  
  <pre><code>@using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;div class="form-group"&gt;
          &lt;div class="input-group"&gt;
              &lt;span class="input-group-addon"&gt;£&lt;/span&gt;
              &lt;input type="number" class="form-control" value="@Model.NextBid" id="CurrentBid" name="CurrentBid"/&gt;
          &lt;/div&gt;
          &lt;br /&gt;
          &lt;button type="submit" class="btn btn-primary btn-lg"&gt;Place bid&lt;/button&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>Using this code allows me to write a controller method with this signature:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Bid([Bind(Include = "ID,CurrentBid")] Item item)
  </code></pre>
  
  <p>This kind of works so far. But note that I am having to pass the amount that has been bid in the CurrentBid field of the item, before it has been validated server-side. This doesn't feel quite right to me. Is there a way of writing the method so it just takes a signature like this?</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Bid(int itemID, int bidAmount)
  </code></pre>
  
  <p>Maybe there's a way to do that with query strings or something?</p>
  
  <p>Anyway, once inside the method, things again seem a little weird. Protecting from over-posting, the only fields in the <code>item</code> variable that are valid are <code>ID</code> and <code>CurrentBid</code>. So I then do a lookup in the DbContext to find the actual item that corresponds with that ID and update it:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Bid([Bind(Include = "ID,CurrentBid")] Item item)
      {
          if (ModelState.IsValid)
          {
              Item i2 = db.Items.Find(item.ID);
              if (item.CurrentBid &gt;= i2.NextBid)
              {
                  i2.Bids++;
                  i2.CurrentBid = item.CurrentBid;
                  i2.HighBidder = HttpContext.User.Identity.Name;
                  db.Entry(i2).State = EntityState.Modified;
                  db.SaveChanges();
              }
              return View(i2);
          }
          return RedirectToAction("Auction");
      }
  </code></pre>
  
  <p>This seems to work. But it does not feel right. I think I am missing some important concepts/patterns here. If any experienced MVC folks could sketch how they would wire up the client/server logic for this simple button, that would be great. (You would also be helping a good cause!)</p>