gs11111
gs11111

Reputation: 659

Convert HTML form declaration to Razor

How can I convert this line:

<form action="[email protected]
    (Request.QueryString["ReturnUrl"])" method="post" id="openid_form">

...to Razor, e.g. something looks similar to this:

(@using(Html.BeginForm("Authenticate", )

Upvotes: 1

Views: 241

Answers (1)

Tim M.
Tim M.

Reputation: 54377

@using( Html.BeginForm( "Authenticate", "[controller name here]", 
    new { ReturnUrl = HttpUtility.UrlEncode( Request.QueryString["ReturnUrl"] ) }, 
    FormMethod.Post ) ) {

    @* form here *@
}

You want to use the BeginForm() overload which allows route values to be passed, and a form method to be specified. This overload also requires the name of your controller.

Many helper methods use anonymous types as shorthand for name/value pairs.

For example, new { ReturnUrl = "foo" } will be turned into a RouteValueDictionary with a single item having "ReturnUrl" as the key, and "foo" as the value. This will then be provided to a matching action method.

Upvotes: 1

Related Questions