包含模型

时间:2017-04-02 14:57:34

标签: c# asp.net

我想创建一个包含模型中所有属性的POST表单。每个属性都将设置为隐藏输入项:

@using (Html.BeginForm("MyAction", "MyController"))
{           
    @Html.HiddenFor(m => m.Field1)
    @Html.HiddenFor(m => m.Field2)
    ...
    @Html.HiddenFor(m => m.FieldN)          

    <button type="submit" class="btn btn-primary">Launch query</button>
}

理想情况下,我想做(伪代码):

@using (Html.BeginForm("MyAction", "MyController"))
{           
    foreach(var prop in m.Props)
    {
        @Html.HiddenFor(m => m[prop])
    }

    <button type="submit" class="btn btn-primary">Launch query</button>
}

1 个答案:

答案 0 :(得分:2)

可能是这样的:

@foreach (var property in Model.GetType().GetProperties())
{
    <input id="@property.Name" name="@property.Name" value="@property.GetValue(Model, null)" type="hidden" />
}