MVC .net Core2 - 模型在控制器处为空(使用 Jquery ajax 时)
MVC .net Core2 - Model is null at controller (when using Jquery ajax)
我正在使用 asp.net core2 和 jquery ajax
我正在使用 Jquery ajax 到 post 控制器。但是,在控制器操作中,所有值均为 null
然而,当我尝试通过 ajax 将其 post 发送到服务器时,值显示为空
我做错了什么?
这是我的代码
更新:
现在没有控制器操作,我只是检查模型是否不 return null
从更新按钮 class 调用 Jquery 函数
$('.Update_Class').click(function(e) {
e.preventDefault(e);
function ehi(e) {
var fd = new FormData($("#Basket")[0]);
alert('Courtnery');
$.ajax({
type: "POST",
url: "/Shopping_Basket/Shops_AddToBasket_Update",//"/Shops/AddToBasket", //
contentType: false,
processData: false,
data: fd,
success: function (message) {
和查看页面
public class Shops_Basket
{
[Key]
public int BasketID { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public int ProductID { get; set; }
[Required]
public DateTime Dates { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public decimal Price_Unit { get; set; }
public decimal Total { get; set; }
public bool IsCompleted { get; set; }
public Products products { get; set; }
public decimal Basket_Total
{
get { return Quantity * Price_Unit; }
// set { Basket_Total = Basket_Total; }
}
public decimal Invoice
{
get { return Quantity * Price_Unit; }
// set { Basket_Total = Basket_Total; }
}
最后是查看页面
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Dates)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.Price_Unit)
</th>
<th>
@Html.DisplayNameFor(model => model.Total)
</th>
<th>
@Html.DisplayNameFor(model => model.IsCompleted)
</th>
<th>
@Html.DisplayNameFor(model => model.products)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Dates)
<img src="/images/Department_Categories/@(item.ProductID).png" alt="@(item.products.Product_Name)" class="img-fluid img-thumbnail_50 rounded" />
</td>
<td>
<input asp-for="@item.Quantity" class="col-2 form-control75 col-md-5" type="number" autocomplete="off" style="width:20px;" min="1" size="5" max="10000" value="1" />
<input type="hidden" asp-for="@item.ProductID" class="ProductID" />
<input class="Update_Class" id="Update" type="submit" value="Update" name="Submit" />
<td>
@Html.DisplayFor(modelItem => item.Price_Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Total)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsCompleted)
</td>
<td>
@Html.DisplayFor(modelItem => item.products.ProductID)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.BasketID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.BasketID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.BasketID">Delete</a>
</td>
</tr>
我解决了这个问题,我删除了 formdata 部分(发送整个表单)并将其作为 Json 类型发送,这是最终代码
type: "POST",
url: "/shops/test",
dataType: 'json',
data: data,
success: function (message) {
我正在使用 asp.net core2 和 jquery ajax
我正在使用 Jquery ajax 到 post 控制器。但是,在控制器操作中,所有值均为 null
然而,当我尝试通过 ajax 将其 post 发送到服务器时,值显示为空 我做错了什么?
这是我的代码
更新: 现在没有控制器操作,我只是检查模型是否不 return null
从更新按钮 class 调用 Jquery 函数
$('.Update_Class').click(function(e) {
e.preventDefault(e);
function ehi(e) {
var fd = new FormData($("#Basket")[0]);
alert('Courtnery');
$.ajax({
type: "POST",
url: "/Shopping_Basket/Shops_AddToBasket_Update",//"/Shops/AddToBasket", //
contentType: false,
processData: false,
data: fd,
success: function (message) {
和查看页面
public class Shops_Basket
{
[Key]
public int BasketID { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public int ProductID { get; set; }
[Required]
public DateTime Dates { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public decimal Price_Unit { get; set; }
public decimal Total { get; set; }
public bool IsCompleted { get; set; }
public Products products { get; set; }
public decimal Basket_Total
{
get { return Quantity * Price_Unit; }
// set { Basket_Total = Basket_Total; }
}
public decimal Invoice
{
get { return Quantity * Price_Unit; }
// set { Basket_Total = Basket_Total; }
}
最后是查看页面
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Dates)
</th>
<th>
@Html.DisplayNameFor(model => model.Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.Price_Unit)
</th>
<th>
@Html.DisplayNameFor(model => model.Total)
</th>
<th>
@Html.DisplayNameFor(model => model.IsCompleted)
</th>
<th>
@Html.DisplayNameFor(model => model.products)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Dates)
<img src="/images/Department_Categories/@(item.ProductID).png" alt="@(item.products.Product_Name)" class="img-fluid img-thumbnail_50 rounded" />
</td>
<td>
<input asp-for="@item.Quantity" class="col-2 form-control75 col-md-5" type="number" autocomplete="off" style="width:20px;" min="1" size="5" max="10000" value="1" />
<input type="hidden" asp-for="@item.ProductID" class="ProductID" />
<input class="Update_Class" id="Update" type="submit" value="Update" name="Submit" />
<td>
@Html.DisplayFor(modelItem => item.Price_Unit)
</td>
<td>
@Html.DisplayFor(modelItem => item.Total)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsCompleted)
</td>
<td>
@Html.DisplayFor(modelItem => item.products.ProductID)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.BasketID">Edit</a> |
<a asp-action="Details" asp-route-id="@item.BasketID">Details</a> |
<a asp-action="Delete" asp-route-id="@item.BasketID">Delete</a>
</td>
</tr>
我解决了这个问题,我删除了 formdata 部分(发送整个表单)并将其作为 Json 类型发送,这是最终代码
type: "POST",
url: "/shops/test",
dataType: 'json',
data: data,
success: function (message) {