如何使用Html.ValidationSummary只显示一个错误讯息? [英] How to display only one error message using Html.ValidationSummary?

查看:936
本文介绍了如何使用Html.ValidationSummary只显示一个错误讯息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个观点(跳过冗余code为简单起见):

  @model CompetencyModel
@using(Html.BeginForm())
{
    @ Html.ValidationSummary()
    @ Html.EditorFor(型号=> model.Results)
}

当我提出,如果验证在 EditorFor 失败,的ValidationSummary 显示所有验证消息为每个项目从<$ C $到来的列表C> EditorFor 。

这截图是一个视图,其中 EditorFor 是单选按钮一行:

但我需要一个单错误信息随便说什么失败。我想:

  @ Html.ValidationSummary(真)
@ Html.EditorFor(型号=&GT; model.Results)

但它并不显示任何东西。我试过

  @ Html.ValidationSummary(真)
@ Html.EditorFor(型号=&GT; model.Results)
@ Html.ValidationMessageFor(型号=&GT; model.Results,ERROR !!!)

但它并不显示任何东西。

我是什么做错了吗?谢谢你。

解决方案
这将这样的伎俩。

 &LT;风格&GT; .validation-汇总错误UL {显示:无; }&LT; /风格&GT;
@ Html.ValidationSummary(不能保存,必须回答所有问题!)


解决方案

ValidationExtensions.ValidationSummary方法总是返回项目的未排序列表。

另一个人问这个问题,并选择使用jQuery隐藏物品:
<一href=\"http://stackoverflow.com/a/10837702/1220302\">http://stackoverflow.com/a/10837702/1220302

另一种解决方案,你可以做的只是利用CSS的的第n个孩子的伪标记。当然,这取决于你使用虽然浏览器版本:

我觉得是这样的:

  .validation-汇总错误UL李:第n个孩子(-n + 1){
    显示:无;
}

I have a view (skipping redundant code for simplicity):

@model CompetencyModel
@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    @Html.EditorFor(model => model.Results)
}

When I submit, if validation fails inside EditorFor, ValidationSummary shows the list of all validation messages for each item coming from EditorFor.

This screenshot is a view, where EditorFor is a single line with radio buttons:

But I need a single error message just saying that something failed. I tried:

@Html.ValidationSummary(true)
@Html.EditorFor(model => model.Results)

But it doesn't show anything. I tried

@Html.ValidationSummary(true)
@Html.EditorFor(model => model.Results)
@Html.ValidationMessageFor(model => model.Results, "ERROR !!!")

But it doesn't show anything.

What am I doing wrong? Thank you.

Solution This will do the trick.

<style>.validation-summary-errors ul { display: none; }</style>
@Html.ValidationSummary("Can't save, you must answer all questions !!")

解决方案

The ValidationExtensions.ValidationSummary method will always return an un-ordered list of items.

Another person asked this question and choose to use JQuery to hide the items: http://stackoverflow.com/a/10837702/1220302

Another solution you could do is only display the first item in the list using CSS's nth-child pseudo tag. This of course is dependent on the browser version you're using though:

I think something like this:

.validation-summary-errors ul li:nth-child(-n+1) {
    display:none;
}

这篇关于如何使用Html.ValidationSummary只显示一个错误讯息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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