ASP.NET MVC记录状态
本文关键字:状态 记录 MVC NET ASP | 更新日期: 2023-09-27 17:49:24
我正在ASP中构建拍卖系统。净MVC。
如何让我的项目类的状态显示"完成",当结束日期已经过去。
到目前为止,我已经创建了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; }
}
我很难理解你在问什么,但是假设你想只允许出价时,拍卖还没有完成然后添加一个小的形式竞标到您的项目详细信息视图(拍卖项目页面),它传递一个Bid CreateModel或ItemID +出价金额在控制器上的出价动作。
你的控制器动作可以像这样:
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()
或者您可以创建计算列或视图,以便在运行时计算数据库中的值。