DataAnnotation 属性在 Asp.Net MVC2 中不起作用
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您的回答。我发现了问题所在。伙伴类中的属性需要是公共的。在我将财产公开后,一切都开始运作。
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.
嗯,您发布的代码看起来很可靠。您的视图是否是强类型的并继承了适当的类?
另外,这可能听起来像一个愚蠢的问题,但是当您第一次填充视图和将其发回时,您是否将填充的 Player 对象传递回视图?最后你确定要检查一下吗
Hmm, the code you posted look pretty solid. Is your view strongly typed and inheriting the appropriate class?
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