Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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
C# MVC 5 ASP.NET街道地址验证_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# MVC 5 ASP.NET街道地址验证

C# MVC 5 ASP.NET街道地址验证,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我有以下配置文件类: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace MyValidation.Models { public class Profile : ValidationAttribute { [Required]

我有以下配置文件类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MyValidation.Models
{
    public class Profile : ValidationAttribute
    {

        [Required]
        public string Name
        {
            get;
            set;
        }

        [Required]
        [Range(5, 99)]
        public int Age
        {
            get;
            set;
        }


        public string Street
        {
            get;
            set;
        }
        public string City
        {
            get;
            set;
        }

        public string Zip
        {
            get;
            set;
        }

        public string State
        {
            get;
            set;
        }
    }
}
和视图:

@model MyValidation.Models.Profile

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary()
            <p>@Html.ValidationMessageFor(x => x.Name)</p>
            <p>Your Full Name: @Html.TextBoxFor(x => x.Name)</p>
            <p>@Html.ValidationMessageFor(x => x.Age)</p>
            <p>Your current age: @Html.TextBoxFor(x => x.Age)</p>
            <p>Your full address: 
            <br />@Html.TextBoxFor(x => x.Street)</p>
            @:City: @Html.TextBoxFor(x => x.City)
            @:State: @Html.TextBoxFor(x => x.State)
            @:Zip Code: @Html.TextBoxFor(x => x.Zip)
            <input type="submit" value="Submit Form" />
        }
    </div>
</body>
</html>

我想我找到了答案,但我不确定这是不是一种“正确”的方法?

这种方法不是正确的方法,因为| |将返回false,即使所有条件都为false,如果其中一个条件为true,则返回true。 以下是我如何知道这会起作用的

if((!string.IsNullOrEmpty(_profile.Street) || !string.IsNullOrEmpty(_profile.State) || !string.IsNullOrEmpty(_profile.City) || !string.IsNullOrEmpty(_profile.Zip)) && (string.IsNullOrEmpty(_profile.Street) || string.IsNullOrEmpty(_profile.State) || string.IsNullOrEmpty(_profile.City) || string.IsNullOrEmpty(_profile.Zip)))
{
ModelState.AddModelError("Address", "All Address fields have to be empty, OR full");
}

删除
:ValidationAttribute
,因为您的模型类不是属性。谢谢,那么验证属性的意义是什么?如果您正在进行自定义验证?
if((!string.IsNullOrEmpty(_profile.Street) || !string.IsNullOrEmpty(_profile.State) || !string.IsNullOrEmpty(_profile.City) || !string.IsNullOrEmpty(_profile.Zip)) && (string.IsNullOrEmpty(_profile.Street) || string.IsNullOrEmpty(_profile.State) || string.IsNullOrEmpty(_profile.City) || string.IsNullOrEmpty(_profile.Zip)))
{
ModelState.AddModelError("Address", "All Address fields have to be empty, OR full");
}