【问题标题】:MVC .net Core2 - Model is null at controller (when using Jquery ajax)MVC .net Core2 - 控制器上的模型为空(使用 Jquery ajax 时)
【发布时间】:2018-10-28 16:29:17
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 控制器动作是什么样的?
  • 你在哪里调用ehi JavaScript 方法?
  • id="Basket" 的元素是什么?而且您不能使用`foreach 循环为集合生成表单控件(它生成与您的模型无关的name 属性)。请参阅Post an HTML Table to ADO.NET DataTable。而且我们甚至不知道您的 POST 方法是什么!
  • @StephenMuecke 我没有使用 ADO.NET 而是 MVC Datacontext,post 方法也是通过 ajax jquery 而不是控制器操作,因此是 e.preventdefault
  • @Shyju 和 cal5barton 对您的上述问题进行了更新

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多