一些值是对控制器ASP.Net Core的空AJAX调用 [英] Some value are null AJAX call to controller ASP.Net Core
问题描述
我正在尝试通过AJAX调用将对象通过表单传递给控制器.
I'm trying to pass an object through a form to my controller with an AJAX call.
这里是对象,除AuctionId
之外,所有内容均返回null
/0
:
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; }
}
表格:
<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);
}
});
});
然后我们在控制器中执行操作:
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);
}
所以问题是,某些值而不是所有值都返回null
.
So the problem is, that some value, not all values returns null
.
为什么不是AuctionId
null
,而其他人是?
Why isn't AuctionId
null
but the others are?
我还尝试制作一个新的ViewModel,因为我已经在视图中拥有了一个ViewView拍卖工具.我通过拍卖和出价制作了一个新的 我使表单看起来像这样:
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>
但是现在一切都为空
推荐答案
在清除一些代码后,此方法可以正常工作.
This works fine after some code cleanup.
客户端-View
和JS
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>
服务器端-Controller
和Model
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);
}
}
}
这篇关于一些值是对控制器ASP.Net Core的空AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!