MVC Razor - BeginForm 与锚动作
MVC Razor - BeginForm with Anchor Action
我在页面底部附近有一个联系表,需要滚动才能到达;提交表单后,页面会重新加载,我需要将页面转到联系人表单的锚点。当我更改 BeginForm 的操作时,锚点中的“#”被编码为“%23” - 如何停止这种形式的发生?
我需要它在不使用任何 JavaScript 的情况下运行。
@{
using (Html.BeginForm(null, null, new { ReturnURL = "#contact" }, FormMethod.Post, new { novalidate = "novalidate" }))
{
//Fields...
<button type="submit">Submit</button>
}
}
上面的代码会return下面HTML,不会去主播:
<form action="/en/Home/Contact?ReturnURL=%23contact" method="post" novalidate="novalidate">
虽然下面的 HTML 会(但我不知道如何用 Razor 生成它):
<form action="/en/Home/Contact?ReturnURL=#contact" method="post" novalidate="novalidate">
我也试过以下方法,但“#”总是被编码:
using (Html.BeginForm(null, null, new { ReturnURL = HttpUtility.HtmlDecode("#contact") }, FormMethod.Post, new { novalidate = "novalidate" }))
using (Html.BeginForm(null, null, new { ReturnURL = HttpUtility.UrlDecode("#contact") }, FormMethod.Post, new { novalidate = "novalidate" }))
using (Html.BeginForm(new { action = Url.Action("Index", "Home") + Html.Raw("Contact/#contact") }))
似乎使用关键字 'action' 覆盖了表单的操作 - 下面的代码是一个解决方案:
using (Html.BeginForm(null, null, FormMethod.Post, new { action = "#contact" }))
提交后,用户将被发送到 site.com/#contact
对于表单不在 index/home/front 页面上的站点,我使用以下方法构建了一个路由:
using (Html.BeginForm(null, null, FormMethod.Post,
new { action = string.Format("{0}/{1}#contact",
ViewContext.RouteData.Values["controller"],
ViewContext.RouteData.Values["action"])
}))
我在页面底部附近有一个联系表,需要滚动才能到达;提交表单后,页面会重新加载,我需要将页面转到联系人表单的锚点。当我更改 BeginForm 的操作时,锚点中的“#”被编码为“%23” - 如何停止这种形式的发生?
我需要它在不使用任何 JavaScript 的情况下运行。
@{
using (Html.BeginForm(null, null, new { ReturnURL = "#contact" }, FormMethod.Post, new { novalidate = "novalidate" }))
{
//Fields...
<button type="submit">Submit</button>
}
}
上面的代码会return下面HTML,不会去主播:
<form action="/en/Home/Contact?ReturnURL=%23contact" method="post" novalidate="novalidate">
虽然下面的 HTML 会(但我不知道如何用 Razor 生成它):
<form action="/en/Home/Contact?ReturnURL=#contact" method="post" novalidate="novalidate">
我也试过以下方法,但“#”总是被编码:
using (Html.BeginForm(null, null, new { ReturnURL = HttpUtility.HtmlDecode("#contact") }, FormMethod.Post, new { novalidate = "novalidate" }))
using (Html.BeginForm(null, null, new { ReturnURL = HttpUtility.UrlDecode("#contact") }, FormMethod.Post, new { novalidate = "novalidate" }))
using (Html.BeginForm(new { action = Url.Action("Index", "Home") + Html.Raw("Contact/#contact") }))
似乎使用关键字 'action' 覆盖了表单的操作 - 下面的代码是一个解决方案:
using (Html.BeginForm(null, null, FormMethod.Post, new { action = "#contact" }))
提交后,用户将被发送到 site.com/#contact
对于表单不在 index/home/front 页面上的站点,我使用以下方法构建了一个路由:
using (Html.BeginForm(null, null, FormMethod.Post,
new { action = string.Format("{0}/{1}#contact",
ViewContext.RouteData.Values["controller"],
ViewContext.RouteData.Values["action"])
}))