我正在ASP.NET MVC中构建拍卖系统。
如何在结束日期过后,让我的Item类的状态显示“已完成”。
到目前为止,我已经创建了Item模型:
public class Item
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ItemId { get; set; }
[StringLength(100, ErrorMessage = "The {0} must be between {1} and {2} characters length", MinimumLength = 6)]
public string Title { get; set; }
[DataType(DataType.DateTime)]
public DateTime StartDate { get; set; }
[DataType(DataType.DateTime)]
public DateTime EndDate { get; set; }
public decimal StartingPrice { get; set; }
[StringLength(4000, ErrorMessage = "Description must be no longer than {1}")]
public string Description { get; set; }
public string Image { get; set; }
public string Status { get; set; }
public virtual UserProfile UserProfile { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
}
和ViewModel:
public class ViewItemViewModel
{
// [HiddenInput(DisplayValue = false)]
public int ItemId { get; set; }
public string Title { get; set; }
private DateTime endDate;
[Required]
[DataType(DataType.DateTime)]
public DateTime EndDate
{
get { return endDate; }
set
{
TimeSpan span = (value - DateTime.Now);
TimeLeft = String.Format("{0} d, {1} h, {2} m, {3} s left", span.Days, span.Hours, span.Minutes, span.Seconds);
endDate = value;
}
}
public string TimeLeft { get; private set; }
[StringLength(4000, ErrorMessage = "Description must be no longer than {1}")]
public string Description { get; set; }
public string Image { get; set; }
}
答案 0 :(得分:0)
我很难理解您的要求,但是假设您只想在拍卖未完成时允许出价,然后在项目详细信息视图中添加一个小表单进行出价(拍卖物品页面),将出价创建模型或项目ID +出价金额传递给控制器上的出价操作。
您的控制器操作可能类似于:
public ActionResult Bid(int itemid, decimal bidAmount)
{
Item item = db.Items.Find(itemid);
if (item.EndDate < DateTime.Now)
{
// Too late, the auction is over
return View("AuctionComplete");
}
if (bidAmount > item.Bids.OrderByDescending(b => b.Value).First().Value);
{
// Valid bid, add to db and return view
db.Bids.Add(new Bid() { ItemID = itemid, Value = bidAmount });
db.SaveChanges();
return View();
}
// There is a higher bid than this one already
return View("Outbid");
}
修改强>
public string Status { get { return EndDate < DateTime.Now ? "Finished" : "Not Finished"; } }
我个人会实现一个名为'Finished'或'Complete'的布尔属性,然后在其他计算/决策中使用它。例如:
public bool Finished { get { return EndDate < DateTime.Now; } }
public string Status { get { return Finished ? "Finished" : "Live"; } }
修改2
您可以运行一个服务,使用以下内容查询数据库:
UPDATE Items SET Status = 'Finished' WHERE EndDate < GETDATE()
或者您可以创建计算列或视图以在运行时计算数据库中的值。