I am making an auction application. Currently I am working on the bidding system. My Auction model consists of:
public class Auctions
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
(...) some more fields like title, description etc(...)
public List<Bid> bids = new List<Bid>(); // a list of bids from the users
}
public class Bid
{
public string bidAuthor { get; set; }
public decimal bid { get; set; }
public DateTime bidDate { get; set; }
}
In the view, I have a form for sending a bid:
@model BiddingViewModel
(...)info stuff about the auction(...)
@using (Html.BeginForm("CreateBid", "Auction", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.auctionToSend.ID)
@Html.EditorFor(model => model.bid)
<input type="submit" value="Send a bid" />
}
Then, my contoller:
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> CreateBid(BiddingViewModel bvm)
{
var user = await _userManager.FindByIdAsync(HttpContext.User.GetUserId());
var tmp = _context.Auctions.FirstOrDefault(i => i.ID == bvm.auctionToSend.ID);
Bid newBid = new Bid()
{
bid = decimal.Parse(bvm.bid.ToString()),
bidAuthor = user.Email,
bidDate = DateTime.Now
};
tmp.bids.Add(newBid);
_context.Entry(tmp).State = Microsoft.Data.Entity.EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("AuctionList", "Auction");
}
Unfortunately, this doesn't seem to update my bids
column in my database (which is of type VARBINARY(MAX)
). What am I doing wrong?