现在它仅将AuctionId值输入控制器,其他属性为null或0 ...
这是表格:
<form id="createBid">
<div id="frmBid" class="form-inline">
<input name="Bidder" asp-for="@bidModel.Bidder" value="@User.Identity.Name" type="hidden" />
<input name="AuctionId" asp-for="@bidModel.AuctionId" value="@Model.AuctionId" type="hidden" id="auctionId" />
<label asp-for="@bidModel.Amount" />
<input name="Amount" asp-for="@bidModel.Amount" />
<button type="submit" id="submitBtn" class="btn btn-primary">Lägg</button>
</div>
</form>
这是控制器中的动作:
public async Task<IActionResult> AddBid(BidModel Bid)
{
var result = await _bidBusinessInterface.CreateBidAsync(Bid, Bid.AuctionId);
if (result)
{
ViewBag.Message = "Bud lagt!";
}
else
{
ViewBag.Message = "Bud förlågt!";
}
return RedirectToAction("ViewDetails");
}
然后我们进行了实际的AJAX调用:
$('#createBid').on('submit', function (e)
{
e.preventDefault();
var $form = $(this);
$.ajax({
url: '@Url.Action("AddBid")',
type: 'POST',
dataType: 'html',
data: $form.serialize(),
success: function (html)
{
$('#frmBid').html(html);
}
});
});
如果需要查看模型出问题的地方,我也会在此处发布模型:
public class BidModel
{
[JsonProperty("BudID")]
public string BidId { get; set; }
[JsonProperty("Summa")]
public int Amount { get; set; }
[JsonProperty("AuktionID")]
public string AuctionId { get; set; }
[JsonProperty("Budgivare")]
public string Bidder { get; set; }
}
每一个答案我都很感激!这已经困扰了我两个小时。