6

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
} 
2
  • It appears to work just fine on my end. But there's no BeginForm<T> available (i made a quick helper method to test) so are you using an external library or something? What's the error in the Razor Syntax? Commented Jul 6, 2011 at 17:19
  • We're using the MvcContrib extensions from Microsoft.Web.Mvc.dll, which contains a number of strongly typed html helpers. The error is that @(Html.BeginForm<T>(...)) works fine but @using(Html.BeginForm<T>(...)) does not and I can't figure out a way to format it so that it would work. Commented Jul 7, 2011 at 5:20

1 Answer 1

8

What if you wrap the whole statement in parens:

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

Or use a code block?

HTH.

Sign up to request clarification or add additional context in comments.

2 Comments

I've tried that and it's no good. Added examples of different attempts above.
Works for me in MVC 4.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.