![](/img/trans.png)
[英]ASP.NET Core MVC controller receives null for input parameter from ajax call
[英]Some value are null AJAX call to controller ASP.Net Core
我正在嘗試使用 AJAX 調用將對象通過表單傳遞到我的控制器。
這是對象,除AuctionId
外,所有內容都返回null
/ 0
:
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; }
}
表格:
<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>
這是 AJAX 調用:
$('#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);
}
});
});
然后我們在控制器中有動作:
[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);
}
所以問題是,某些值,而不是所有值都返回null
。
為什么AuctionId
不為null
而其他為null
?
我還嘗試創建一個新的 ViewModel,因為我已經在視圖中將 Auction 作為 Viewmodel。 我用拍賣和出價做了一個新的,我把表格變成了這樣:
<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>
但現在一切都為空
這在一些代碼清理后工作正常。
客戶端 - View
和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>
服務器端 - Controller
和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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.