MVC .net Core2-控制器上的模型为null(使用Jquery ajax时)

时间:2018-10-28 16:29:18

标签: jquery asp.net-mvc asp.net-core-2.0

我正在使用asp.net core2和jquery ajax

我正在使用Jquery ajax发布到控制器。但是,在控制器操作中,所有值都为空

但是,当我尝试通过ajax将其发布到服务器时,这些值显示为null 我在做什么错了?

这是我的代码

更新: 现在没有控制器动作,我只是在检查模型是否不返回null

按此从更新按钮类中调用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>

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我删除了formdata部分(发送整个表单),并将其作为Json类型发送,这是最终代码

type: "POST",
    url: "/shops/test",   
    dataType: 'json',
    data: data,
success: function (message) {