DataAnnotation 属性在 Asp.Net MVC2 中不起作用

发布于 2024-09-08 14:05:38 字数 2026 浏览 7 评论 0原文

我在使用 mvc2 时遇到问题。

我使用实体框架作为我的 ORM。我使用 POCO 实体生成器来创建 POCO 对象。我将生成的对象移动到一个单独的项目中。我按照此处概述的步骤进行操作(

Sports.Data - 实体框架和数据访问

Sports.Entities - 生成的 poco 对象和好友类

Sports.Web - Web 应用程序

在 Sports.Entities 项目中,我为生成的 pocos 创建了好友类。

我遇到的问题是 mvc 忽略了属性。验证不起作用,并且未显示正确的标签文本。显示“衬衫尺寸”而不是“衬衫尺寸”。

我做错了什么?

感谢您的任何帮助。

这是代码。

<div>
    <%: Html.LabelFor(m => m.Player.ShirtSize)%>
</div>
<div class="editor-field">
    <%: Html.EditorFor(m => m.Player.ShirtSize) %>
    <%: Html.ValidationMessageFor(m => m.Player.ShirtSize)%>
<div>

    namespace Sports.Entities
{
    [MetadataType(typeof(PlayerMetadata))]
    public partial class Player
    {
        public double PointsPerGame { 
            get { return (double) this.Points / this.MatchesPlayed; }
        }
    }
    public class PlayerMetadata
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "First Name is required.")]
        [StringLength(50, ErrorMessage = "First Name must be 50 characters or less")]
        [DataType(DataType.Text)]
        object FirstName { get; set; }
        
        [Required(AllowEmptyStrings = false, ErrorMessage = "Last Name is required.")]
        [StringLength(50, ErrorMessage = "First Name must be 50 characters or less")]
        [DataType(DataType.Text)]
        object LastName { get; set; }
        
        [Required(ErrorMessage = "Shirt Size is required.")]
        [EnumDataType(typeof(ShirtSizeEnum), ErrorMessage = "Shirt Size is invalid.")]
        [DisplayName("Shirt Size")]
        object ShirtSize { get; set; }
        
        [Required(ErrorMessage = "Gender is required.")]
        [EnumDataType(typeof(GenderEnum) , ErrorMessage = "Gender is invalid.")]
        object Gender { get; set; }
        
        [Required(AllowEmptyStrings = false , ErrorMessage = "Date of birth is required.")]
        [DataType(DataType.Date)]
        object DateOfBirth { get; set; }
    }
}

I am having trouble with the with mvc2.

I am using the Entity Framework as my ORM. I have used the POCO Entity Generator to create POCO objects. I moved the generated objects to a separate project. I followed steps outlined here(Tutorial). This is my project structure

Sports.Data - Entity Frmework and Data Access

Sports.Entities - The poco objects generated and buddy classes

Sports.Web - The web application

In the Sports.Entities project I have created buddy classes for the generated pocos.

The problem I am having is that the attributes are being ignored by the mvc. The validation is not working and the correct label text is not being displayed. Instead of 'Shirt Size', 'ShirtSize' is displayed.

What am I doing wrong?

Thanks for any help.

Here is the code.

<div>
    <%: Html.LabelFor(m => m.Player.ShirtSize)%>
</div>
<div class="editor-field">
    <%: Html.EditorFor(m => m.Player.ShirtSize) %>
    <%: Html.ValidationMessageFor(m => m.Player.ShirtSize)%>
<div>

    namespace Sports.Entities
{
    [MetadataType(typeof(PlayerMetadata))]
    public partial class Player
    {
        public double PointsPerGame { 
            get { return (double) this.Points / this.MatchesPlayed; }
        }
    }
    public class PlayerMetadata
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "First Name is required.")]
        [StringLength(50, ErrorMessage = "First Name must be 50 characters or less")]
        [DataType(DataType.Text)]
        object FirstName { get; set; }
        
        [Required(AllowEmptyStrings = false, ErrorMessage = "Last Name is required.")]
        [StringLength(50, ErrorMessage = "First Name must be 50 characters or less")]
        [DataType(DataType.Text)]
        object LastName { get; set; }
        
        [Required(ErrorMessage = "Shirt Size is required.")]
        [EnumDataType(typeof(ShirtSizeEnum), ErrorMessage = "Shirt Size is invalid.")]
        [DisplayName("Shirt Size")]
        object ShirtSize { get; set; }
        
        [Required(ErrorMessage = "Gender is required.")]
        [EnumDataType(typeof(GenderEnum) , ErrorMessage = "Gender is invalid.")]
        object Gender { get; set; }
        
        [Required(AllowEmptyStrings = false , ErrorMessage = "Date of birth is required.")]
        [DataType(DataType.Date)]
        object DateOfBirth { get; set; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的新欢旧爱 2024-09-15 14:05:38

感谢您的回答。我发现了问题所在。伙伴类中的属性需要是公共的。在我将财产公开后,一切都开始运作。

public object ShirtSize { get; set; }

Thanks for your answer. I figured out the problem. The properties in the buddy classes need to be public. Everything started working after I made the properties public.

public object ShirtSize { get; set; }
凡尘雨 2024-09-15 14:05:38

嗯,您发布的代码看起来很可靠。您的视图是否是强类型的并继承了适当的类?

Inherits="System.Web.Mvc.ViewPage(Of YourWebApp.Sports.Entities.Player)

另外,这可能听起来像一个愚蠢的问题,但是当您第一次填充视图和将其发回时,您是否将填充的 Player 对象传递回视图?最后你确定要检查一下吗

 if ModelState.IsValid?

Hmm, the code you posted look pretty solid. Is your view strongly typed and inheriting the appropriate class?

Inherits="System.Web.Mvc.ViewPage(Of YourWebApp.Sports.Entities.Player)

also, this might sound like a dumb question, but are you passing the populated Player object back to the view both when you first populate the view and when you post it back? and finally are you making sure to check

 if ModelState.IsValid?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文