Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/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
C# 使用分部类进行验证?_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 使用分部类进行验证?

C# 使用分部类进行验证?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在使用实体框架创建数据对象。下面是我的designer.cs文件的外观: namespace MyApp.WebUI.Models { ... [EdmEntityTypeAttribute(NamespaceName="MyAppDBModel", Name="AddressType")] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class

我正在使用实体框架创建数据对象。下面是我的designer.cs文件的外观:

namespace MyApp.WebUI.Models
{
    ...

    [EdmEntityTypeAttribute(NamespaceName="MyAppDBModel", Name="AddressType")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class AddressType : EntityObject
    {
        ...
    }

    ...
}
namespace MyApp.WebUI.Models
{
    public class Validations
    {
        ...

        [MetadataType(typeof(AddressTypesValidation))]
        public partial class AddressType
        {

        }

        public class AddressTypesValidation
        {
            [Required(ErrorMessage = "Address Type name is required.")]
            [StringLength(50, ErrorMessage = "Address Type name must be 50 characters or less.")]
            public string Name { get; set; }                
        }
    }
}
我有一个名为Validation.cs的文件,我想在其中保留我对实体的所有验证。下面是它的样子:

namespace MyApp.WebUI.Models
{
    ...

    [EdmEntityTypeAttribute(NamespaceName="MyAppDBModel", Name="AddressType")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class AddressType : EntityObject
    {
        ...
    }

    ...
}
namespace MyApp.WebUI.Models
{
    public class Validations
    {
        ...

        [MetadataType(typeof(AddressTypesValidation))]
        public partial class AddressType
        {

        }

        public class AddressTypesValidation
        {
            [Required(ErrorMessage = "Address Type name is required.")]
            [StringLength(50, ErrorMessage = "Address Type name must be 50 characters or less.")]
            public string Name { get; set; }                
        }
    }
}
我的查看页面中有以下内容:

<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Edit", "AddressTypes", FormMethod.Post)) { %>

    <div class="editor-label"><%: Html.LabelFor(m => m.Name) %></div>
    <div class="editor-field">
        <%: Html.TextBoxFor(m => m.Name) %>
        <%: Html.ValidationMessageFor(m => m.Name) %>
    </div>
    <input type="submit" value="Save" />

<% } %>

m、 名称)%%>
m、 名称)%%>
m、 名称)%%>
但我的验证没有加载。如果我尝试提交没有名称值的表单,我会收到一条错误消息,指出值“”无效。而不是我的错误消息


我做错了什么?

我不确定是否可以有一个分部类,然后有另一个分部类是嵌套类。尝试使您声明的部分类不是嵌套类

编辑:
只需在VS(不是验证部分)中运行一个快速测试,就不能将分部类的一部分嵌套在嵌套类型中,而将分部类的另一部分作为非嵌套类型(或嵌套在其他类型中)。

我在这里可能有点不对劲,但我相信
Required
属性只是表示“notnull”,而您的
StringLengthValidator
只检查上限。它没有失败,因为您是通过字符串发送的-不幸的是,它是
字符串。空的

你在使用过载

[StringLength(int upperBound, [Parameters])]
相反,试试看

[StringLength(int lowerBound, int upperBound, [Parameters])]
类似于此,如果您希望最小长度为1:

[Required(ErrorMessage = "Address Type name is required.")]               
[StringLength(1, 50, ErrorMessage = "Address Type...")]
public string Name { get; set; } 

你的方法有一个根本缺陷。一般认为,将数据库对象用作视图模型并让Mvc对其进行模型绑定是一个非常糟糕的想法。

Darin详细介绍了在视图中使用域对象的相关问题

我认为您的问题是因为您将数据对象与视图模型混合在一起,引用Darin的话

大约60%的问题我是[达林] 在系统中回答StackOverflow问题 asp.net-mvc标记不会被删除 询问OP是否使用了视图 模型


我同意使用viewmodels是一种方法,但是否仍然可以对业务对象本身进行验证?viewmodel最终封装了这些相同的属性,我认为最好将验证放在一个位置(靠近数据库),而不是跨多个可能以UI为中心的实现进行复制。