将HTML表单声明转换为Razor

时间:2013-06-01 08:04:47

标签: html asp.net asp.net-mvc-3 razor

如何转换此行:

<form action="Authenticate?ReturnUrl=@HttpUtility.UrlEncode
    (Request.QueryString["ReturnUrl"])" method="post" id="openid_form">

......对Razor,例如看起来像这样:

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

1 个答案:

答案 0 :(得分:1)

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

    @* form here *@
}

您希望使用允许传递路由值的BeginForm()重载,以及要指定的表单方法。此重载还需要控制器的名称。

许多辅助方法使用匿名类型作为名称/值对的简写。

例如,new { ReturnUrl = "foo" }将变为RouteValueDictionary,其中单个项目具有“ReturnUrl”作为键,“foo”作为值。然后,这将提供给匹配的操作方法。