Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using with generic methods in razor view

Is there a way to make the using statement work with generic methods in razor views? For example I'd like the webforms snippet

<% using(Html.BeginForm<Controller>(c => c.Method()))
   { %>
       Some code
<% } %>

converted to razor like this

@using(Html.BeginForm<Controller>(c => c.Method()))
{
    Some code
}

but that does not work, since razor interprets <Controller> as an HTML tag. Adding parentheses does not work either, since then razor does not include the curly brackets that begin and end the BeginForm. Below are the different approaches I've tried, and I can't think of any more.

@using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c# to '<Controller>'
{                                                   // interpreted as HTML
    Some code                                       // interpreted as HTML
}                                                   // interpreted as HTML

@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c#
{                                                     // interpreted as HTML
    Some code                                         // interpreted as HTML
}                                                     // interpreted as HTML

@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c#
    {                                                // interpreted as c#
        Some code                                    // interpreted as c#
    }                                                // interpreted as c#
}                                                    // interpreted as c#

@(using(Html.BeginForm<Controller>(c => c.Method()))) // Interpreted as c#
@{                                                    // interpreted as c#
        Some code                                     // interpreted as c#
}                                                     // interpreted as c#    

Does aynone know how to do this?

Update: It seems the third way above is the way to do this. Razor apparently works like this:

@{using(Html.BeginForm<Controller>(c => c.Method())) // Interpreted as c#
    {                                                // interpreted as c#
        <p>                                          // interpreted as HTML
        Some text                                    // interpreted as HTML
        @Code                                        // interpreted as c#
        </p>                                         // interpreted as HTML
    }                                                // interpreted as c#
}                                                    // interpreted as c#

Not the most obvious way of doing things, but it works.

Update 2: Razor has probably been updated at some point, because now option #1 above works as expected.

@using(Html.BeginForm<Controller>(c => c.Method()))
{
    Some code
} 
like image 929
nforss Avatar asked Jul 06 '11 09:07

nforss


1 Answers

What if you wrap the whole statement in parens:

@(using(Html.BeginForm<Controller>(c => c.Method())))

Or use a code block?

HTH.

like image 56
Brian Mains Avatar answered Nov 10 '22 06:11

Brian Mains