使用带有隐藏字段和复选框的 IEnumerable<T> 和 foreach 无法将参数从视图传递到控制器
Use of IEnumerable<T> and foreach with hidden fields and checkbox not working to pass parameters from view to controller
我想要实现的是向用户显示一组复选框,其中包含 <span>
ProductName 和一个布尔值(如果 ProductName 是否有货)。我还希望这些复选框在同一视图中更新 table。如果有更新,我想使用主键 ProductKey 更新 SQL 中的 table。
我有一个 class 产品包含此信息:
public class Products
{
[Key]
public string ProductKey {get;set;}
public string ProductName {get;set;}
public Boolean IsAvailable {get;set;}
}
实际上这个问题有点复杂,因为不同的产品可以有不同的属性。为了解决这个问题,我找到了一个创建 IEnumerable 模型的解决方案。这是在对此问题感兴趣的视图之前生成的。
我的问题是我希望能够遍历我的 IEnumerable 模型,查看产品名称是否有货(表明复选框是否被选中)并能够从相同的观点。
@model IEnumerable<Products>
<form asp-controller="Product" asp-action="FindIfProductInStock">
<div class="row">
@foreach (var item in Model)
{
<div class="form-group">
<label class="label-checkbox">
<input asp-for=@item.IsAvailable type="checkbox" checked=@item.IsAvailable />
<span>
@Html.DisplayFor(modelItem => item.ProductName)
</span>
</label>
</div>
<div class="form-group">
<input asp-for=@item.ProductKey type="hidden" value=@item.ProductKey />
</div>
}
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</form>
我的 FindIfProductInStock
控制器看起来像这样:
[HttpPost]
public IEnumerable<Products> FindIfProductInStock(IEnumerable<Products> productList)
{
return productList;
}
(我现在使用这个控制器能够创建一个断点来查看 productList 的实际值是什么)。
呈现给用户的视图按预期工作。如果产品有库存,复选框会被选中。我的问题是 productList 不包含任何数据。它有 0 行,IsAvailable 为 false,Products 的其余值为空。有谁知道为什么?
我将 type="hidden"
用于 ProductKey,因为我不希望用户看到该字段,但我需要它将 IsAvailable
的值连接到我 SQL table 稍后。
用for循环替换foreach
@for (int i = 0; i < Model.Count; i+=1)
{
.....
<input asp-for=@Model[i].IsAvailable type="checkbox" checked=@Model[i].IsAvailable />
....
@Html.DisplayFor(m => m[i].ProductName)
.... and so on
}
我想要实现的是向用户显示一组复选框,其中包含 <span>
ProductName 和一个布尔值(如果 ProductName 是否有货)。我还希望这些复选框在同一视图中更新 table。如果有更新,我想使用主键 ProductKey 更新 SQL 中的 table。
我有一个 class 产品包含此信息:
public class Products
{
[Key]
public string ProductKey {get;set;}
public string ProductName {get;set;}
public Boolean IsAvailable {get;set;}
}
实际上这个问题有点复杂,因为不同的产品可以有不同的属性。为了解决这个问题,我找到了一个创建 IEnumerable 模型的解决方案。这是在对此问题感兴趣的视图之前生成的。
我的问题是我希望能够遍历我的 IEnumerable 模型,查看产品名称是否有货(表明复选框是否被选中)并能够从相同的观点。
@model IEnumerable<Products>
<form asp-controller="Product" asp-action="FindIfProductInStock">
<div class="row">
@foreach (var item in Model)
{
<div class="form-group">
<label class="label-checkbox">
<input asp-for=@item.IsAvailable type="checkbox" checked=@item.IsAvailable />
<span>
@Html.DisplayFor(modelItem => item.ProductName)
</span>
</label>
</div>
<div class="form-group">
<input asp-for=@item.ProductKey type="hidden" value=@item.ProductKey />
</div>
}
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-primary" />
</div>
</form>
我的 FindIfProductInStock
控制器看起来像这样:
[HttpPost]
public IEnumerable<Products> FindIfProductInStock(IEnumerable<Products> productList)
{
return productList;
}
(我现在使用这个控制器能够创建一个断点来查看 productList 的实际值是什么)。
呈现给用户的视图按预期工作。如果产品有库存,复选框会被选中。我的问题是 productList 不包含任何数据。它有 0 行,IsAvailable 为 false,Products 的其余值为空。有谁知道为什么?
我将 type="hidden"
用于 ProductKey,因为我不希望用户看到该字段,但我需要它将 IsAvailable
的值连接到我 SQL table 稍后。
用for循环替换foreach
@for (int i = 0; i < Model.Count; i+=1)
{
.....
<input asp-for=@Model[i].IsAvailable type="checkbox" checked=@Model[i].IsAvailable />
....
@Html.DisplayFor(m => m[i].ProductName)
.... and so on
}