Pergunta

I have a view which look like this

    @model UI.Models.BidOnAuctionViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@{
    var disableButton = Model.AuctionOpen ? "" : "bid-btn-disabled";         
}

@using (Html.BeginForm("ConfirmBid", "Lot"))
{
    <fieldset>
        @Html.HiddenFor(model => model.AuctionId)

        <p>
            <label >&pound;@Model.NextBidAmount</label>
        </p>
        <p>
            @if (Model.AuctionOpen)
            {
                 <input type="submit" value="Place bid" class="bid-btn @disableButton" />
            }
        </p>
    </fieldset>
}

When I click on the Place bid button, I go to the confirmBid action method

[Authorize]
[HttpPost]
public ActionResult ConfirmBid(BidOnAuctionViewModel model)

The problem is that even though in the view the model was not null, for some reason it is null in this action method. Why is this?

Foi útil?

Solução

After analyzing your code i found the issue. Actually you are just displaying the value of NextBidAmount. And when form get post again the model object lost its value.

To persist its value on post in model just create a hidden field for it as you already done for AuctionId

@Html.HiddenFor(model => model.NextBidAmount)

This will fix your issue.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top