ASP.NET MVC - 发布形式为HTML书签? [英] ASP.NET MVC - Post form to html bookmark?

查看:229
本文介绍了ASP.NET MVC - 发布形式为HTML书签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用形式是这样的:

<h2>
<%:Model.EulaTitle %>
</h2>
<p>
<%=Model.EulaHtml %>
</p>
<a name="errors"></a>
<%:Html.ValidationSummary()%>


<div style="text-align:center;">
<% using (Html.BeginForm())
   { %>

<%:Html.HiddenFor(model => model.SourceUrl)%>
<%:Html.HiddenFor(model => model.EulaId)%>


<a name="accept"></a>
<div style="text-align:center;">
<%:Html.CheckBoxFor(model => model.Accepted)%>
<%:Html.LabelFor(model => model.Accepted)%>
</div>
<input type="submit" value="Submit">
<% } %>
</div>

我需要的页面时,它的职位滚动到 #errors。在 Model.EulaHtml 包含一些长度EULA文字,我会preFER用户不必手动向下滚动才能看到一个错误信息,如果他们发布页面,而不接受该协议。

I need the page to scroll to #errors when it posts. The Model.EulaHtml contains some length EULA text and I'd prefer that the user not have to manually scroll down to see an error message if they post the page without accepting the agreement.

如果控制器检测到 ModelState.IsValid 上发表,它重定向到另一个页面。如果没有,我需要留在此页面上,但滚动到 #errors 书签锚标记。

If the controller detects ModelState.IsValid on Post, it redirects to another page. If not, I need to stay on this page, but scroll to the #errors bookmark anchor tag.

我想过只需添加#errors到表单动作的URL的结尾,但是我沿着有潜在危险的......(%的行收到错误)。这可能是我正确编码#号。任何人都不得不面对呢?我们正在处理的浏览器兼容性(IE6 +,并在阳光下一切)pretty限制性要求,所以我尽量避免使用JavaScript只要有可能。

I've thought about just adding '#errors' to the end of the url in the form action, but I receive errors along the line of a potentially dangerous .... ('%'). It's possible that I'm incorrectly encoding the hash mark. Anyone else had to deal with this? We're dealing with pretty restrictive requirements for browser compatibility (IE6+ and everything else under the sun) so I try to avoid using JavaScript whenever possible.

更新

我收到的错误是:

A potentially dangerous Request.Path value was detected from the client (%).

我体改的 Html.BeginForm()来电

<% using (Html.BeginForm(new { @action="#errors" }))
   { %>

和产生的URL是:

http://..../TheControllerName/Eula/%2523errors/

我也注意到,正在穿过几个参数的queryString消失的时候我设置了动作属性,以这种方式。 (这并不令人惊讶,但没有修复因为这是显而易见的我)

I also noticed that a few queryString parameters that were being passed through disappear when I set the action attribute in this way. (Not surprising, but no fix for that is immediately obvious to me)

推荐答案

尝试这样的:

<form action="<%: Url.Action("actionName", "controllerName") %>#errors" method="post">

    <%:Html.HiddenFor(model => model.SourceUrl)%>
    <%:Html.HiddenFor(model => model.EulaId)%>


    <a name="accept"></a>
    <div style="text-align:center;">
    <%:Html.CheckBoxFor(model => model.Accepted)%>
    <%:Html.LabelFor(model => model.Accepted)%>
    </div>
    <input type="submit" value="Submit">

</form>


您也可以写一个自定义的HTML帮助,将做的工作:


You could also write a custom HTML helper that will do the job:

public static class FormExtensions
{
    public static MvcForm MyBeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, string fragment)
    {
        var formAction = UrlHelper.GenerateUrl(null, actionName, controllerName, htmlHelper.ViewContext.HttpContext.Request.Url.Scheme, null, fragment, null, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
        var builder = new TagBuilder("form");
        builder.MergeAttribute("action", formAction);
        builder.MergeAttribute("method", "post", true);
        htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
        return new MvcForm(htmlHelper.ViewContext);
    }
}

和则:

<% using (Html.MyBeginForm("index", "home", "errors")) { %>
    ...
<% }

这篇关于ASP.NET MVC - 发布形式为HTML书签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆