C# 在mvc中使用Checkbox
我的ProductListController中有这个类。使用以下操作,我填充上面的类并将其传递给视图C# 在mvc中使用Checkbox,c#,model-view-controller,checkbox,C#,Model View Controller,Checkbox,我的ProductListController中有这个类。使用以下操作,我填充上面的类并将其传递给视图 public class ProdList { public int ProdId { get; set; } [DisplayName("Name")] public string ProdName { get; set; } [DisplayName("Price")] public double?
public class ProdList
{
public int ProdId { get; set; }
[DisplayName("Name")]
public string ProdName { get; set; }
[DisplayName("Price")]
public double? ProdPrice { get; set; }
[DisplayName("Description")]
public string ProdDesc { get; set; }
[DisplayName("Image")]
public string ImageVal { get; set; }
[DisplayName("Category Name")]
public string CateName { get; set; }
}
我尝试了各种方法来包含复选框,并在控制器中检索已选中的项目,但没有任何帮助。我真不知道该怎么做。请帮帮我 首先,向模型/视图模型添加布尔属性:
[HttpPost]
public ActionResult checkboxhandle(ProdList pdl)
{
/*Logic to get the checked items from the list in the view*/
}
然后,您需要对列表执行for循环,以便model binder可以在表单发布时填充项目,如:
public bool SelectedProduct {get;set;}
@for(int i=0;i
@Html.DisplayFor(modelItem=>Model[i].ProdName)
@DisplayFor(modelItem=>Model[i].ProdPrice)
@DisplayFor(modelItem=>Model[i].ProdDesc)
@DisplayFor(modelItem=>Model[i].CateName)
@Html.CheckBoxFor(modelItem=>Model[i].SelectedProduct)
@ActionLink(“编辑”,“编辑”,新的{id=Model[i].ProdId})|
@ActionLink(“详细信息”,“详细信息”,新的{id=Model[i].ProdId})|
}
use for loop为值编制索引,以便模型绑定器可以填充对象。它在for循环中给了我一个错误。它说“实际上,我将模型转换为一个列表。var prodlst=model.ToList();我的循环现在看起来像这样,但我仍然得到一个错误。@for(int i=0;itry我更新的代码,您不需要将其存储在单独的变量中。我首先尝试了更新的代码,但它在for循环中给出了一个错误。它说“让我们”。
[HttpPost]
public ActionResult checkboxhandle(ProdList pdl)
{
/*Logic to get the checked items from the list in the view*/
}
public bool SelectedProduct {get;set;}
@for(int i=0; i< Model.Count; i++)
{
<tr>
<td>
<img src="@Url.Content(@Html.DisplayFor(modelItem => Model[i].ImageVal).ToString())" height="150" width="150">
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].ProdName)
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].ProdPrice)
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].ProdDesc)
</td>
<td>
@Html.DisplayFor(modelItem => Model[i].CateName)
</td>
<td>
@Html.CheckBoxFor(modelItem => Model[i].SelectedProduct)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = Model[i].ProdId }) |
@Html.ActionLink("Details", "Details", new { id = Model[i].ProdId }) |
</td>
</tr>
}