ChickenOverlord
ChickenOverlord

Reputation: 162

ASP.NET Core 2.2 id passed to controller is always 0

I'm creating an auction website and when submitting a bid, I'm trying to pass the id of the auction to the bid form so that I can associate the bid with an auction.

However, no matter what I do, the auction id being passed to the EnterBid controller is always 0. specifically the auctionItemId and auctionStartingBid in the EnterBid controller are always 0.

Here's my code:

EnterBid controller:

[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 controller (for viewing details on an auction, contains the link to enter the bid):

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();
}

Details 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 view:

@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");}
}

Upvotes: 0

Views: 835

Answers (1)

ChickenOverlord
ChickenOverlord

Reputation: 162

So I was able to figure it out, I needed to add a route attribute tag before the EnterBid controller, so it now looks like this:

[HttpPost]
    [ValidateAntiForgeryToken]
    [Route("Search/EnterBid/{auctionItemId}")]
    public async Task<IActionResult> EnterBid(int auctionItemId, [Bind("BidId,BidderName,BidderPhone,BidderEmail,BidderAddress,BidAmount,AuctionItemModel")] AuctionBidModel auctionBidModel)

I still have some strange issues if I try to set it up for both parameters (the id and the starting price) but otherwise it works, and I have a workaround for that in the meantime.

Upvotes: 1

Related Questions