0

我正在使用 ASP.Net MVC 和 jQuery 不显眼的客户端验证。我有一个这样的按钮:

<input type="submit" name="SubmitButton" value="Add Item" class="cancel" />

通常,提交按钮会发出 POST 调用以将新项目添加到列表中。显然,出于这个目的,我不需要验证,这很好。

问题是,在页面重新加载之前,所有字段上的验证错误消息都会短暂显示!

我已经搜索过是否是不显眼的验证脚本导致了这个,或者它是 jQuery 验证的错误。

有任何想法吗?


更新:为了更好地澄清我的问题:

期望的状态是:当按钮被标记为“取消”时,表单被回发而不显示任何错误消息。

当前状态是:当按钮被标记为“取消”时,表单被回发但显示错误消息!!

4

1 回答 1

0

我建议不要使用提交类型的输入。而是尝试使用 html 锚(链接) - 或 MVC 中的 Html.ActionLink 重新加载页面或重定向到某个地方。
此处发布了类似的问题:表单中的取消按钮

更新 1 - 根据@sam360 评论

您可以尝试将 MultiButtonAtribute 放在应该处理取消按钮请求的操作方法上:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
    public MultiButtonAttribute(string matchFormKey) : this(matchFormKey, null) {
    }

    public MultiButtonAttribute(string matchFormKey, string matchFormValue) {
        this.MatchFormKey = matchFormKey;
        this.MatchFormValue = matchFormValue;
    }

    public string MatchFormKey { get; set; }
    public string MatchFormValue { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        string value = controllerContext.HttpContext.Request[MatchFormKey];
        return value != null && (value == MatchFormValue || MatchFormValue == null);
    }
} 

如果按钮的名称(“取消”)将在请求中匹配,则取消操作将处理该请求。

[HttpPost]
[MultiButton("cancel")]
public ActionResult CancelAction(MyModel model)
{
 // ... update the model (e.g. remove the last item from list and return the view with updated model
}

在表格中,您将拥有:

<input type="submit" name="cancel" value="Remove Item" class="cancel" />

但如果我理解得很好。您想在某种形式上动态添加/删除输入。您应该考虑在客户端使用 javascript 或 jQuery 函数来保存一些服务器请求(但如果您想支持未启用 javascript 的客户端,这可能不是您的选择)。

于 2012-11-10T13:21:43.300 回答