Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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 3 asp.net mvc3中的自定义验证程序_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 asp.net mvc3中的自定义验证程序

Asp.net mvc 3 asp.net mvc3中的自定义验证程序,asp.net-mvc-3,Asp.net Mvc 3,我在asp.net mvc3应用程序中创建了一个自定义验证器,如下所示: { if (customerToValidate.FirstName == customerToValidate.LastName) return new ValidationResult("First Name and Last Name can not be same."); return ValidationResult.Success; } publi

我在asp.net mvc3应用程序中创建了一个自定义验证器,如下所示:

{
  if (customerToValidate.FirstName == customerToValidate.LastName)
            return new ValidationResult("First Name and Last Name can not be same.");

        return ValidationResult.Success;
    }

    public static ValidationResult ValidateFirstName(string firstName, ValidationContext context)
    {
        if (firstName == "Nadeem")
        {
            return new ValidationResult("First Name can not be Nadeem", new List<string> { "FirstName" });
        }
        return ValidationResult.Success;
    }
[CustomValidation(typeof(CustomerValidator), "ValidateCustomer")]
public class Customer
{
    public int Id { get; set; }

    [CustomValidation(typeof(CustomerValidator), "ValidateFirstName")]
    public string FirstName { get; set; }

    public string LastName { get; set; }
}
@model CustomvalidatorSample.Models.Customer
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (@Html.BeginForm())
{
    @Html.ValidationSummary(false)

    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName, "First Name")
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
    </div>


    <div class="editor-label">
        @Html.LabelFor(model => model.LastName, "Last Name")
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
    </div>

    <div>
    <input type="submit" value="Validate" />
    </div>
}
我的看法是这样的:

{
  if (customerToValidate.FirstName == customerToValidate.LastName)
            return new ValidationResult("First Name and Last Name can not be same.");

        return ValidationResult.Success;
    }

    public static ValidationResult ValidateFirstName(string firstName, ValidationContext context)
    {
        if (firstName == "Nadeem")
        {
            return new ValidationResult("First Name can not be Nadeem", new List<string> { "FirstName" });
        }
        return ValidationResult.Success;
    }
[CustomValidation(typeof(CustomerValidator), "ValidateCustomer")]
public class Customer
{
    public int Id { get; set; }

    [CustomValidation(typeof(CustomerValidator), "ValidateFirstName")]
    public string FirstName { get; set; }

    public string LastName { get; set; }
}
@model CustomvalidatorSample.Models.Customer
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (@Html.BeginForm())
{
    @Html.ValidationSummary(false)

    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName, "First Name")
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
    </div>


    <div class="editor-label">
        @Html.LabelFor(model => model.LastName, "Last Name")
    </div>

    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
    </div>

    <div>
    <input type="submit" value="Validate" />
    </div>
}
@model CustomvalidatorSample.Models.Customer
@{
ViewBag.Title=“Index”;
}
指数
@使用(@Html.BeginForm())
{
@Html.ValidationSummary(false)
@Html.LabelFor(model=>model.FirstName,“First Name”)
@EditorFor(model=>model.FirstName)
@Html.LabelFor(model=>model.LastName,“LastName”)
@EditorFor(model=>model.LastName)
}
但验证不会启动。请提出解决方案


谢谢

你怎么知道验证没有启动?您是否在控制器中设置断点

您没有在视图中显示任何验证错误。您需要将以下行添加到视图中

@Html.ValidationMessageFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.LastName)
您将希望从类中删除自定义验证。但请将其保留在属性上