views:

89

answers:

1

Hi,

Did anyone ever manage to create forms within the grid?

Attempt (which does 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

A: 

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.

Darin Dimitrov
Thanks. I will have a look at this tomorrow. My view is strongly typed but I have to use ViewData in this case as the data represents a page of the child objects of the strongly typed view's object. you commented on a related question of mine today which explains this ...
csetzkorn