MVC.NETCore2-模型在控制器处为空(使用jQueryAjax时)
我正在使用asp.NETCore2和jQueryAjax 我正在使用jQueryAjax发布到控制器。但是,在控制器操作中,所有值都为空 但是,当我试图通过ajax将其发布到服务器时,值显示为null 我做错了什么 这是我的密码 更新: 目前还没有控制器操作,我只是检查模型是否没有返回null Jquery函数是从updatebutton类调用的,如下所示MVC.NETCore2-模型在控制器处为空(使用jQueryAjax时),jquery,asp.net-mvc,asp.net-core-2.0,Jquery,Asp.net Mvc,Asp.net Core 2.0,我正在使用asp.NETCore2和jQueryAjax 我正在使用jQueryAjax发布到控制器。但是,在控制器操作中,所有值都为空 但是,当我试图通过ajax将其发布到服务器时,值显示为null 我做错了什么 这是我的密码 更新: 目前还没有控制器操作,我只是检查模型是否没有返回null Jquery函数是从updatebutton类调用的,如下所示 $('.Update_Class').click(function(e) { e.preventDefault(e);
$('.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>
@DisplayNameFor(model=>model.Dates)
@DisplayNameFor(model=>model.Quantity)
@Html.DisplayNameFor(model=>model.Price\u单位)
@DisplayNameFor(model=>model.Total)
@DisplayNameFor(model=>model.IsCompleted)
@DisplayNameFor(model=>model.products)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.Dates)
@DisplayFor(modelItem=>item.Price\u单位)
@DisplayFor(modelItem=>item.Total)
@DisplayFor(modelItem=>item.IsCompleted)
@DisplayFor(modelItem=>item.products.ProductID)
编辑|
细节|
删除
我解决了这个问题,删除了formdata部分(它发送整个表单)并将其作为Json类型发送,这是最后的代码
type: "POST",
url: "/shops/test",
dataType: 'json',
data: data,
success: function (message) {
控制器操作看起来像什么?您在哪里调用
ehi
JavaScript方法?使用id=“Basket”
的元素是什么?并且您不能使用`foreach
循环为集合生成表单控件(其生成的name
属性与您的模型没有关系)。请参阅。我们甚至不知道您的POST方法是什么!@StephenMuecke我使用的不是ADO.NET而是MVC Datacontext,而且POST方法是通过ajax jquery而不是控制器操作,因此e。preventdefault@Shyju以及cal5barton对上述问题的更新