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
}
What if you wrap the whole statement in parens:
@(using(Html.BeginForm<Controller>(c => c.Method())))
Or use a code block?
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With