我正在创建一个拍卖网站,并且在提交投标时,我试图将拍卖的ID传递到投标表单,以便可以将投标与拍卖相关联。
但是,无论我做什么,传递给EnterBid控制器的拍卖ID始终为0。特别是EnterBid控制器中的auctionItemId和AuctionStartingBid始终为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);
}
Details控制器(用于查看拍卖的详细信息,包含用于输入出价的链接):
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");}
}
答案 0 :(得分:0)
所以我能够弄清楚,我需要在EnterBid控制器之前添加一个route属性标签,所以现在看起来像这样:
[HttpPost]
[ValidateAntiForgeryToken]
[Route("Search/EnterBid/{auctionItemId}")]
public async Task<IActionResult> EnterBid(int auctionItemId, [Bind("BidId,BidderName,BidderPhone,BidderEmail,BidderAddress,BidAmount,AuctionItemModel")] AuctionBidModel auctionBidModel)
如果我尝试同时为两个参数(id和起始价格)设置它,我仍然会遇到一些奇怪的问题,但是否则它可以工作,同时我也有解决方法。