post items of webgrid asp.net mvc3

2020-03-04 04:02发布

I have these DTO's

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; }
}

and I have this Controller

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

and this 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")
    )

}

So, this grid have a checkbox and a HiddenField that hold the HeaderItemId value of each row.

I would like to post my form and have my property HeaderItems of Header class filled.

How could I reach this solution?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-03-04 04:14

Try change your Html.CheckBox for Html.CheckBoxFor and the same for Html.Hidden for Html.HiddenFor.

It would be:

@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")
    )
)

I have not tested it thought...

查看更多
家丑人穷心不美
3楼-- · 2020-03-04 04:19

How could I reach this solution?

Like this:

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")
)
查看更多
登录 后发表回答