Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 始终显示验证错误_Asp.net Mvc_Validation - Fatal编程技术网

Asp.net mvc 始终显示验证错误

Asp.net mvc 始终显示验证错误,asp.net-mvc,validation,Asp.net Mvc,Validation,我有一个MVC5应用程序。我正在尝试使用viewmodel属性应用验证。例如,我的字段是必填字段。但由于某些原因,在用户可以输入任何内容之前,空字段上的错误最初会显示出来。你能解释一下吗 @Html.ValidationSummary(false, "Please fix the errors:", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model

我有一个MVC5应用程序。我正在尝试使用viewmodel属性应用验证。例如,我的字段是必填字段。但由于某些原因,在用户可以输入任何内容之前,空字段上的错误最初会显示出来。你能解释一下吗

@Html.ValidationSummary(false, "Please fix the errors:", new { @class = "text-danger" })
<div class="form-group">
    @Html.LabelFor(model => model.FileToUpload, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBox("FileToUpload", "", new { @type = "file", @class = "input-xlarge form-control" })
        @Html.ValidationMessageFor(model => model.FileToUpload, "*", new { @class = "text-danger" })
    </div>
</div>

我使用viewmodel仅用于验证目的,因此它不会传递给视图。

正如我所评论的,一种解决方案是添加以下css:

.validation-summary-valid
{
display:none;
}
但另一种方法是检查视图中是否有错误,然后显示:

@if (ViewData.ModelState.Any(x => x.Value.Errors.Any()))
{
    @Html.ValidationSummary(false, "Please fix the errors:", new { @class = "text-danger" })    
}

可能重复的谢谢!非常好。对于总是显示的@Html.ValidationMessageFor(),这对我来说很有效:。字段验证有效{display:none;}
@if (ViewData.ModelState.Any(x => x.Value.Errors.Any()))
{
    @Html.ValidationSummary(false, "Please fix the errors:", new { @class = "text-danger" })    
}