使用 Html helper HiddenFor C# MVC 将模型添加到模型中
Add model into model using Html helper HiddenFor C# MVC
我有一个像
这样的模型
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
以在 table 中显示数据。
根据 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)
}
}
我认为它不起作用,因为我应该以某种方式将这些属性添加到 Model
中的 OtherModel
;但是我现在拥有它的方式是将属性添加到 Model
.
你可以这样做:
@model Model
@foreach (var item in Model.List)
{
if (item.IsPropTrue)
{
@Html.HiddenFor(model => model.List[Model.List.IndexOf(item)].Value1)
@Html.HiddenFor(model => model.List[Model.List.IndexOf(item)].Value2)
}
}
这样绑定系统会将隐藏字段与模型中的 List OtherModel 绑定
如果你想根据模型将数组发送到服务器,你必须在 @Html.HiddenFor 中使用索引器。
@model WebApplication1.Models.MyModel
<form>
@if (Model != null && Model.List != null)
{
for (int i = 0; i < Model.List.Count; i++)
{
if (Model.List[i].IsPropTrue)
{
@Html.HiddenFor(model => Model.List[i].Value1)
@Html.HiddenFor(model => Model.List[i].Value2)
}
}
}
<button type="submit">submit</button>
</form>
如果您想知道在模型上使用索引器的原因,我推荐 MVC 4 列表模型绑定如何工作?
考虑是否由视图或控制器操作来做出决定 - 您可以将所有内容发送回操作来做出决定。
在您的 Views/Shared 文件夹中,创建一个名为 EditorTemplates 的控制器
在此文件夹中,添加一个名为 OtherModel 的分部视图
在此视图中,将模型设置为 OtherModel 并设置 Layout=null
在 EditorFor 中添加三个 OtherModel 字段(如果不显示 isPropTrue,则添加 HiddenFor)。此部分视图仅显示列表的一个实例。
在您的主视图中,像这样使用上面的编辑器模型。 MVC 将处理完整项目列表的模型状态的所有呈现和回发。我们喜欢单线...
@Html.EditorFor(model => model.OtherModel)
当数据随后回发到操作时,Model State 再次将所有显示的项目打包到一个列表中,因此您可以检查服务器上每个项目的 isPropTrue 值。
MVC 的唯一问题是您将一个空列表传递给视图,返回一个空值,因此在返回空值时将其替换为一个空列表
我有一个像
这样的模型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
以在 table 中显示数据。
根据 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)
}
}
我认为它不起作用,因为我应该以某种方式将这些属性添加到 Model
中的 OtherModel
;但是我现在拥有它的方式是将属性添加到 Model
.
你可以这样做:
@model Model
@foreach (var item in Model.List)
{
if (item.IsPropTrue)
{
@Html.HiddenFor(model => model.List[Model.List.IndexOf(item)].Value1)
@Html.HiddenFor(model => model.List[Model.List.IndexOf(item)].Value2)
}
}
这样绑定系统会将隐藏字段与模型中的 List OtherModel 绑定
如果你想根据模型将数组发送到服务器,你必须在 @Html.HiddenFor 中使用索引器。
@model WebApplication1.Models.MyModel
<form>
@if (Model != null && Model.List != null)
{
for (int i = 0; i < Model.List.Count; i++)
{
if (Model.List[i].IsPropTrue)
{
@Html.HiddenFor(model => Model.List[i].Value1)
@Html.HiddenFor(model => Model.List[i].Value2)
}
}
}
<button type="submit">submit</button>
</form>
如果您想知道在模型上使用索引器的原因,我推荐 MVC 4 列表模型绑定如何工作?
考虑是否由视图或控制器操作来做出决定 - 您可以将所有内容发送回操作来做出决定。
在您的 Views/Shared 文件夹中,创建一个名为 EditorTemplates 的控制器 在此文件夹中,添加一个名为 OtherModel 的分部视图 在此视图中,将模型设置为 OtherModel 并设置 Layout=null 在 EditorFor 中添加三个 OtherModel 字段(如果不显示 isPropTrue,则添加 HiddenFor)。此部分视图仅显示列表的一个实例。
在您的主视图中,像这样使用上面的编辑器模型。 MVC 将处理完整项目列表的模型状态的所有呈现和回发。我们喜欢单线...
@Html.EditorFor(model => model.OtherModel)
当数据随后回发到操作时,Model State 再次将所有显示的项目打包到一个列表中,因此您可以检查服务器上每个项目的 isPropTrue 值。
MVC 的唯一问题是您将一个空列表传递给视图,返回一个空值,因此在返回空值时将其替换为一个空列表