【问题标题】:Jump to error after form post mvc3 .net表单发布 mvc3 .net 后跳转到错误
【发布时间】:2012-08-07 23:59:21
【问题描述】:

我有一个打印到屏幕上的数据列表。在屏幕底部(滚动),我有一个带有文本字段的表单,用户可以在其中输入数据。当他们单击提交并且表单无效时,错误会正确显示,但用户现在正在查看在页面的最顶部。因此,他们看不到页面底部有错误。我想让它跳到页面底部的表单,以便用户可以看到发生了错误。

我考虑过使用锚点,但问题是;因为当我意识到表单无效时我已经在控制器中,所以我需要重定向回将主题标签附加到 url 的同一控制器。对我来说,这不是好的做法,而且有点浪费。

有人知道更好的方法吗?

【问题讨论】:

  • 您是否反对客户端验证?这将在他们单击提交而不发布的第二秒显示错误..
  • jQuery 解决方案 - 看看stackoverflow.com/questions/9363508/…
  • 嗯,有两个问题,一个,我链接了MicrosoftAjax.js、MicrosoftMvcAjax.js和MicrosoftMvcValidation.js。我也有: 但是,当我在调试模式下运行时,它仍在打我的控制器。另外,如果他们关闭了 JS,我想要这个解决方案。
  • 一个简单的解决方案是在表单出现错误时在页面顶部添加一条全局错误消息。这通常是我所做的(当我有仅存在于服务器端的验证规则时)
  • 摆脱 microsoftmvc 和 ajax 文件,只使用 jquery 文件,一切仍然正常。没有哈希标签和 JS,你就没有其他跨浏览器选项

标签: .net html asp.net-mvc-3 forms controller


【解决方案1】:

执行此操作的一种方法是始终发布到锚点并在视图中确定放置锚点的位置。如果有模型错误,则将命名锚放在页面底部,如果没有错误,则将锚放在页面顶部。使这一点复杂化的一件事是没有 Html.BeginForm 重载需要一个锚点。你可以像这样简单地实现它

<form action="@Url.Action("actionName", "controllerName")#anchor_name" method="post">

    ..... form contents .....

</form>

或者你可以编写一个自定义的 HTML 助手

public static class FormExtensions
{
    public static MvcForm CustomBeginForm(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);
    }
}

那么你可以使用@Html.CustomBeginForm('action', 'controller', 'anchor_name')

This question 很有用,并告知了我的答案

【讨论】:

  • 谢谢,我最终创建了自己的自定义表单,因为我还希望能够设置表单的 ID。我将在下面发布我的代码。
【解决方案2】:
 public static MvcForm BeginFormAnchor(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, FormMethod method, object htmlAttributes, string hashTag)
    {
        var formAction = UrlHelper.GenerateUrl(null, actionName, controllerName, htmlHelper.ViewContext.HttpContext.Request.Url.Scheme, null, hashTag, ConvertToRouteValueDictionary(routeValues), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
        var builder = new TagBuilder("form");
        builder.MergeAttribute("action", formAction);
        builder.MergeAttribute("method", method.ToString(), true);
        foreach (KeyValuePair<string, string> attribute in ConvertToDictionaryStringString(htmlAttributes))
        {
            builder.MergeAttribute(attribute.Key, attribute.Value);
        }
        htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
        return new MvcForm(htmlHelper.ViewContext);
    }

    private static Dictionary<string, string> ConvertToDictionaryStringString(object obj)
    {
        return obj.GetType().GetProperties().ToDictionary(o => o.Name, o => o.GetValue(obj, null).ToString());
    }

    private static RouteValueDictionary ConvertToRouteValueDictionary(object obj)
    {
        RouteValueDictionary rvd = new RouteValueDictionary();
        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
        foreach (PropertyDescriptor prop in props)
        {
            rvd.Add(prop.Name, prop.GetValue(obj));
        }
        return rvd;
    }

我这样称呼它:

@using (Html.BeginFormAnchor("Action", "Controller", new { id = Model.ID }, FormMethod.Post, new { @id = "formID" }, "anchorName"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多