【问题标题】:Send Viewmodel with list to controller将带有列表的 Viewmodel 发送到控制器
【发布时间】:2018-11-26 13:42:47
【问题描述】:

我正在尝试将具有复杂类型的视图模型提交给我的控制器,但是当它到达控制器时我的视图模型为空。

这是我认为的相关代码:

@model ShoppingCartViewModel

@{
    ViewData["Title"] = "Indkøbskurv";
}

<h2>Indkøbskurv</h2>
<form method="post" asp-controller="ShoppingCart" asp-action="SaveProductsHistory">

    <div class="row" style="margin-bottom:30px">
        <div class="col-md-9"></div>
        <div class="col-md-3">
            <button type="submit" id="saveAndDeleteShoppingCart" class="btn btn-warning">Gem og slet kurv  <i class="fas fa-shopping-cart"></i></button>
        </div>
    </div>
    <table class="table">
        <thead>
            <tr>
                <th>Produktnavn</th>
                <th>Varenummer</th>
                <th>Antal</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @for (int i = 0; i < Model.Products.Count(); i++)
            {               
                <tr>
                    <td>
                        <div class="form-group">
                            <input type="text" asp-for="Products[i].Product.id" />
                            <input type="text" asp-for="Products[i].Product.area" />
                            <input type="text" asp-for="Products[i].Product.eurofinsItemNumber" />
                            <input type="text" asp-for="Products[i].Product.internetAddress" />
                            <input type="text" asp-for="Products[i].Product.ourResponsibility" />
                            <input type="text" asp-for="Products[i].Product.productNameEnvAir" />
                            <input type="text" asp-for="Products[i].Product.productNameSupplier" />
                            <input type="text" asp-for="Products[i].Product.storageSize" />
                            <input type="text" asp-for="Products[i].Product.storageType" />
                            <input type="text" asp-for="Products[i].Product.supplierName" />
                            <input type="text" asp-for="Products[i].Product.supplierProductNumber" />
                            <input type="text" asp-for="Products[i].Product.supplierProductNumberInfo" />
                            <input type="text" asp-for="Products[i].Product.unit" />
                            <input type="text" asp-for="Products[i].Quantity" />
                        </div>
                    </td>
                    <td>
                    </td>
                    <td>
                        <a class="adjust-button dec" aria-label="Decrease quantity" href="#">-</a>
                        <input class="adjust-input" value="@Model.Products[i].Quantity">
                        <input id="productID" type="hidden" value="@Model.Products[i].Product.id" />
                        <a class="adjust-button inc" aria-label="Increase quantity" href="#">+</a>
                    </td>
                    <td>
                        <button id="removeProductFromCookies"><i class="far fa-trash-alt"></i></button>
                    </td>
                </tr>
            }
        </tbody>
    </table>
</form>

连同我对控制器的操作:

 [HttpPost]
 public void SaveProductsHistory(ShoppingCartViewModel shoppingCartViewModels)
        {}

和我的视图模型

public class ShoppingCartViewModel
    {
        public List<ShoppingCartViewModelItem> Products;

        public ShoppingCartViewModel()
        {
            Products = new List<ShoppingCartViewModelItem>();
        }
    }

我的视图很好地显示了我的视图模型中的数据,从检查这篇文章中的第一张图片来看,似乎一切都正确发送了,但是当它到达我的控制器时我仍然收到 null。

【问题讨论】:

  • 尝试在您的操作方法中使用 [FromForm] 属性 [HttpPost] public void SaveProductsHistory([FromForm] ShoppingCartViewModel shoppingCartViewModels) {}
  • 感谢您的建议,但据我所知,asp.net core 2 中没有使用 [FromForm],因为无论如何它都是默认的预期输入。它确实使它更加冗长,以防其他开发人员阅读您的代码。

标签: c# asp.net asp.net-core controller asp.net-mvc-viewmodel


【解决方案1】:

您需要在您的ShoppingCartViewModel 类中将Products 公开为property 而不是field,这使得对象null 作为模型绑定器需要公共设置器来在帖子中填充模型。

变化:

public List<ShoppingCartViewModelItem> Products;

到:

public List<ShoppingCartViewModelItem> Products { get; set; }

编辑:

另一件事我不确定asp-for="Products[i].Product.id" 是否有效,但通常它像asp-for="Model.Products[i].Product.id" 一样提供

希望对你有帮助。

【讨论】:

  • 不得不把 { get;放; } 在所有属性上,在我的 ShoppingCartViewModelItem 上也是如此。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2019-09-22
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多