0
私はオークションアプリケーションを作成しています。現在、私は入札システムに取り組んでいます。その後C#MVC Entity FrameworkがオブジェクトのList属性を更新しない
@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" />
}
、私のcontoller:マイオークションモデルはで構成されていますビューで
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; }
}
、私は入札を送信するためのフォームを持っている
残念ながら[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");
}
、これは」doesnの私のデータベース(VARBINARY(MAX)
)の私のbids
列を更新しているようです。私は間違って何をしていますか?
で説明したuがあなたのコードをデバッグしようとしましたか?いくつかのエラーがありますか? – Alexandr
エンティティを 'EntityState.Modified'に設定する必要はありません.EFがそれを行います。 – DavidG
また、スキーマを表示できますか?その最後の文章は、「入札」がそれ自身のテーブルではなくコラムであることを暗示しているようだ。つまり、本当に奇妙なエラーが出てくるはずです。 – DavidG