在创建Emprestimo的视图中,我有以下代码,显示所有产品,如购物车。当我点击提交时,所有产品都会在表单上发布。但我希望只发布带有值的文本框。
基本上,我希望在此视图中选择一个人在其他地方借用一个或多个产品的产品。但是,并不是必须填写与产品数量相关的所有文本框。
为了更好地理解类,省略了一些属性。
<table class="table table-bordered">
<tr>
<th>Nome</th>
<th>Quantidade</th>
</tr>
@{
var produtos = (List<Produto>)ViewBag.AllProducts;
for (int i = 0; i < produtos.Count; i++)
{
<tr>
@Html.Hidden("Itens[" + i + "].ProdutoId", produtos[i].Id)
<td>@produtos[i].Nome</td>
<td>@Html.TextBox("Itens[" + i + "].Quantidade")</td>
</tr>
}
}
public class Produto
{
public int Id { get; set; }
public string Nome { get; set; }
}
public class Emprestimo
{
public int Id { get; set; }
public virtual ICollection<ItemEmprestimo> Itens { get; set; }
}
public class ItemEmprestimo
{
public int Id { get; set; }
public int Quantidade { get; set; }
public int ProdutoId { get; set; }
public virtual Produto Produto { get; set; }
public int EmprestimoId { get; set; }
public virtual Emprestimo Emprestimo { get; set; }
}