I'm trying to pass an object through a form to my controller with an AJAX call.
Here is the object, everything returns null
/ 0
except AuctionId
:
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; }
}
The Form:
<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>
Here is the AJAX call:
$('#createBid').on('submit', function (e)
{
e.preventDefault();
var $form = $(this);
$.ajax({
url: '@Url.Action("AddBid")',
type: 'POST',
dataType: 'html',
data: JSON.stringify($form.serialize()),
success: function (html)
{
$('#frmBid').html(html);
}
});
});
And then we have the action in the controller:
[HttpPost]
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", Bid.AuctionId);
}
So the problem is, that some value, not all values returns null
.
Why isn't AuctionId
null
but the others are?
I also tried to make a new ViewModel, since I already had an Auction as Viewmodel in the view. I made a new with Auction and a Bid And i make the form look like this:
<form id="createBid">
<div id="frmBid" class="form-inline">
<input asp-for="BidVM.AuctionId" value="AuctionVM.AuctionId" type="hidden" id="auctionId" />
<label asp-for="BidVM.Amount" />
<input asp-for="BidVM.Amount" />
<button type="submit" id="submitBtn" class="btn btn-primary">Lägg</button>
</div>
</form>
But now everything is null
This works fine after some code cleanup.
Client side - View
and JS
@model MyNamespace.BidModel
@{
// Assuming you need to use local variable rather than just @model of BidModel type
// If you can just use model - you will be able to bind imputs like this:
// <input name="@nameof(Model.BidId)" value="@Model.BidId" type="hidden" />
// or
// <input name="BidId" value="@Model.BidId" type="hidden" />
// or (even better)
// just use the tag helper asp-for like this:
// <input asp-for="BidId" type="hidden" />
var bidModel = Model;
}
<form id="createBid">
<div id="frmBid" class="form-inline">
<input name="@nameof(bidModel.Bidder)" value="@bidModel.Bidder" type="hidden" />
<input name="@nameof(bidModel.AuctionId)" value="@bidModel.AuctionId" type="hidden" />
<input name="@nameof(bidModel.BidId)" value="@bidModel.BidId" type="hidden" />
<input name="@nameof(bidModel.Amount)" value="@bidModel.Amount" />
<button type="submit" id="submitBtn" class="btn btn-primary">Add</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#createBid').on('submit',
function(e) {
e.preventDefault();
var $form = $(this);
$.ajax({
url: '@Url.Action("AddBid")',
type: 'POST',
data: $form.serialize(),
success: function(html) {
$('#frmBid').html(html);
}
});
});
});
</script>
Server side - Controller
and Model
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace MyNamespace
{
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; }
}
public class BidController : Controller
{
[HttpGet]
public async Task<IActionResult> AddBid()
{
// For demo purposes - pre-fill some values
var model = new BidModel
{
Bidder = User.Identity.Name,
Amount = 123,
AuctionId = "A-ID",
BidId = "B-ID"
};
return View(model);
}
[HttpPost]
public async Task<IActionResult> AddBid(BidModel Bid)
{
// save new bid
// return RedirectToAction("ViewDetails", Bid.AuctionId);
return View(Bid);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.