Я создаю веб-сайт аукциона и при подаче заявки пытаюсь передать идентификатор аукциона в форму предложения, чтобы связать ставку с аукционом.
Однако, независимо от того, что я делаю, идентификатор аукциона, передаваемый в контроллер EnterBid, всегда равен 0. В частности, auctionItemId и auctionStartingBid в контроллере EnterBid всегда равны 0.
Вот мой код:
Контроллер EnterBid:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EnterBid(int auctionItemId, int auctionStartingBidAmount, [Bind("BidId,BidderName,BidderPhone,BidderEmail,BidderAddress,BidAmount,AuctionItemModel")] AuctionBidModel auctionBidModel)
{
var auctionItemModel = await _context.AuctionItems.FindAsync(auctionItemId);
auctionBidModel.AuctionItemModel = auctionItemModel;
float currentHighestBidAmount = auctionStartingBidAmount;
if (auctionBidModel.AuctionItemModel.Bids != null)
{
foreach (AuctionBidModel bid in auctionBidModel.AuctionItemModel.Bids)
{
if (bid.BidAmount > currentHighestBidAmount)
{
auctionBidModel.AuctionItemModel.CurrentHighestBid = bid;
currentHighestBidAmount = bid.BidAmount;
}
}
}
if (ModelState.IsValid)
{
_context.Add(auctionBidModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View("EnterBid", auctionBidModel);
}
Контроллер деталей (для просмотра деталей на аукционе, содержит ссылку для ввода ставки):
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var auctionItemModel = await _context.AuctionItems.FirstOrDefaultAsync(m => m.ListingId == id);
if (auctionItemModel == null)
{
return NotFound();
}
return View(auctionItemModel);
}
// GET: Search/Create
public IActionResult Create()
{
return View();
}
Просмотр подробностей:
@model Showcase.Models.AuctionItemModel
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>AuctionItemModel</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Name)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.StartingBidAmount)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.StartingBidAmount)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.AuctionStartTime)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.AuctionStartTime)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.AuctionEndTime)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.AuctionEndTime)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Description)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Description)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.ListingImage)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.ListingImage)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.ListingId">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>
<div><a asp-action="EnterBid" asp-route-auctionItemId="@Model.ListingId" asp-route-auctionStartingBidAmount="@Model.StartingBidAmount">Submit a Bid</a></div>
Вид EnterBid:
@model Showcase.Models.AuctionBidModel
@{
ViewData["Title"] = "EnterBid";
}
<h1>EnterBid</h1>
<h4>AuctionBidModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="EnterBid">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="BidderName" class="control-label"></label>
<input asp-for="BidderName" class="form-control" />
<span asp-validation-for="BidderName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BidderPhone" class="control-label"></label>
<input asp-for="BidderPhone" class="form-control" />
<span asp-validation-for="BidderPhone" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BidderEmail" class="control-label"></label>
<input asp-for="BidderEmail" class="form-control" />
<span asp-validation-for="BidderEmail" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BidderAddress" class="control-label"></label>
<input asp-for="BidderAddress" class="form-control" />
<span asp-validation-for="BidderAddress" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="BidAmount" class="control-label"></label>
<input asp-for="BidAmount" class="form-control" />
<span asp-validation-for="BidAmount" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}