【发布时间】:2019-04-02 10:10:34
【问题描述】:
我有一个类似的模型
public class Model
{
public int Value { get; set; }
public List<OtherModel> List { get; set; }
}
public class OtherModel
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public bool IsPropTrue { get; set; }
}
我在一个视图中使用Model,在该视图中循环List 以在表格中显示数据。
根据OtherModel 中的属性之一(IsPropTrue)是真还是假,我想使用HiddenFor Html 帮助器并将数据发送到HttpPost 控制器。
@model Model
@foreach (var item in Model.List)
{
if (item.IsPropTrue)
{
@Html.HiddenFor(model=> item.Value1)
@Html.HiddenFor(model=> item.Value2)
}
}
我认为它不起作用,因为我应该以某种方式将这些属性添加到OtherModel,它位于Model 内;但按照我现在的方式,我正在向Model 添加属性。
【问题讨论】:
-
啊,是的,我在上面犯了一个错误 :) 我在做@foreach(Model.List 中的变量项)
-
我根据 OP 的评论编辑了问题。它现在显示
foreach迭代Model.List。 -
谢谢你,刘易斯 :)
标签: c# html .net model-view-controller html-helper