4

我有这些 DTO

public class Header
{
    public int HeaderId{get;set;}
    public int Description{get;set;}
    public List<HeaderItem> HeaderItems{get;set;}

}

public class HeaderItem
{
    public int HeaderItemId{get;set;}
    public string DetailDescription{ get; set; }
    public bool Selected{ get; set; }
}

我有这个控制器

    [HttpPost]
    public ActionResult PostMethod(Header dto)
    {
        ...
    }

和这个 html

  @using (Html.BeginForm("PostMethod", "Controller", FormMethod.Post, new { id = "form" }))

{

@Html.TextBoxFor(x => x.Description)    

var grid = new WebGrid(Model.HeaderItems);
    }
    @grid.GetHtml(tableStyle: "grid",
    htmlAttributes: new { id = "grid" },
    columns: grid.Columns(
    grid.Column("Selected", "Seç", format: (item) => Html.CheckBox(String.Format("Selected_{0}", (int)item.HeaderItemId), false)),
    grid.Column("HeaderItemId", "", format: (item) => Html.Hidden("HeaderItemId")),
    grid.Column("DetailDescription", "Description")
    )

}

因此,此网格有一个复选框和一个 HiddenField,其中包含每行的 HeaderItemId 值。

我想发布我的表单并填充 Header 类的属性 HeaderItems。

我怎样才能达到这个解决方案?

4

2 回答 2

5

我怎样才能达到这个解决方案?

像这样:

columns: grid.Columns(
    grid.Column(
        "Selected", 
        "Seç", 
        format: 
            @<text>
                @{ var index = Guid.NewGuid().ToString(); }
                @Html.Hidden("HeaderItems.Index", index)
                @Html.Hidden("HeaderItems[" + index + "].HeaderItemId", (int)item.HeaderItemId)
                @Html.CheckBox("HeaderItems[" + index + "].Selected", (bool)item.Selected)
            </text>
    ),
    grid.Column("HeaderItemId")
    grid.Column("DetailDescription", "Description")
)
于 2013-02-27T14:05:46.227 回答
0

尝试更改您的Html.CheckBoxforHtml.CheckBoxFor和相同的Html.Hiddenfor Html.HiddenFor

这将是:

@grid.GetHtml(tableStyle: "grid",
    htmlAttributes: new { id = "grid" },
    columns: grid.Columns(
             grid.Column("Selected", "Seç", format: (item) => Html.CheckBoxFor(m => item.HeaderItemId)),
             grid.Column("HeaderItemId", "", format: (item) => Html.Hidden(m => item.HeaderItemId)),
             grid.Column("DetailDescription", "Description")
    )
)

我没有测试它以为...

于 2013-02-27T14:04:05.547 回答