开发者

mvc contrib grid + forms

开发者 https://www.devze.com 2023-01-06 16:23 出处:网络
Did anyone ever manage to create forms within the grid? Attempt (which doe开发者_运维技巧s not work):

Did anyone ever manage to create forms within the grid?

Attempt (which doe开发者_运维技巧s not work):

 <%= Html.Grid(ViewData["xyz"] as IEnumerable<xyz>).Columns(column =>
   {
    column.For(gf => gf.Value).Named("Value");
    column.For(gf => 
     <% using (Html.BeginForm("deletebla", "controllername", FormMethod.Post, new { @class = "deleteForm" })) 
{ %>  
<input type="submit" value="Delete" /> 
 <% } 
 %>  
).Named("");
 }).Empty("Sorry no data.")%>

Thanks.

Chris


Two possibilities here (in my example I will be using a strongly typed view instead of ViewData in order to promote good practice).

Use the Action Syntax:

<% Html.Grid<UserViewModel>(Model)
    .Columns(column =>
    {
        column.For("Test").Named("Value").Action(p => { %>
            <td>
                <% using (Html.BeginForm("deletebla", "controllername", FormMethod.Post, new { @class = "deleteForm" })) { %>

                <% } %>
            </td>
        <% });
    }).Render();
%>

after adding this in web.config to make sure that proper extension methods are in scope:

<system.web>
  <pages>
    <namespaces>
        <add namespace="MvcContrib.UI.Grid.ActionSyntax" />
    </namespaces>
  </pages>
</system.web>

Or if you want to avoid the tag soup simply use a partial:

<%= Html.Grid<UserViewModel>(Model)
    .Columns(column =>
    {
        column.For("Test").Named("Value").Partial("Foo");
    })
%>

And in Foo.ascx:

<% using (Html.BeginForm("deletebla", "controllername", FormMethod.Post, new { @class = "deleteForm" })) { %>

<% } %>

I would definitely choose the second option.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号