在我的MVC应用程序中,我在域模型中定义了DataAnnotations。尽管在使用Domain模型时可以检索诸如Display等的DataAnnotations属性,但在ViewModel上使用相同的属性并使用此ViewModel时,则无法检索它们。我认为在ViewModel中再次定义DataAnnotations似乎不太好。那么,有可能还是我应该遵循哪种方式?
领域模型:
public class Issue
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Project Number")]
public int ProjectID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Issue Definition")]
public string Description { get; set; }
//... removed for brevity
//Navigation Properties:
public virtual ICollection<FileAttachment> FileAttachments { get; set; }
}
ViewModel:
public class IssueViewModel
{
public int ID { get; set; }
public int ProjectID { get; set; }
public string Description { get; set; }
//... removed for brevity
//Navigation Properties:
public virtual ICollection<FileAttachment> FileAttachments { get; set; }
}
参考方案
您可以创建一个新的伙伴类,其中包含有关属性和类的所有元数据。
public partial class IssueMetadata
{
[Required(ErrorMessage = "Required")]
[Display(Name = "Project Number")]
public int ProjectID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Issue Definition")]
public string Description { get; set; }
}
然后,我们必须通过MetadataType
属性将有关伙伴类的信息告知MVC框架,该属性将伙伴类的类型作为其参数。好友类必须在相同的名称空间中定义,并且
也必须是partial
类。
[MetadataType(typeof(IssueMetadata))]
public partial class IssueViewModel
{
//...
public int ProjectID { get; set; }
public string Description { get; set; }
//...
}
[MetadataType(typeof(IssueMetadata))]
public partial class Issue
{
[Key]
public int ID { get; set; }
public int ProjectID { get; set; }
public string Description { get; set; }
//... removed for brevity
//Navigation Properties:
public virtual ICollection<FileAttachment> FileAttachments { get; set; }
}
附加说明:
如果IssueMetadata
和Issue
(或IssueViewModel
)类位于不同的程序集中,则可以在运行时将这些类与其伙伴类关联,如下所示:
public class AssociatedMetadataConfig
{
public static void RegisterMetadatas()
{
RegisterPairOfTypes(typeof(Issue), typeof(IssueMetadata));
RegisterPairOfTypes(typeof(IssueViewModel), typeof(IssueMetadata));
}
private static void RegisterPairOfTypes(Type mainType, Type buddyType)
{
AssociatedMetadataTypeTypeDescriptionProvider typeDescriptionProvider
= new AssociatedMetadataTypeTypeDescriptionProvider(mainType, buddyType);
TypeDescriptor.AddProviderTransparent(typeDescriptionProvider, mainType);
}
}
并且,只需在global.asax
中调用此静态方法:
AssociatedMetadataConfig.RegisterMetadatas();
ASP.NET MVC 5自定义登录,无需脚手架,数据库优先 - c#我对asp.net和mvc还是很陌生,所以我正在努力学习尽可能多的知识...为此,我从头开始编写博客网站,但是我对身份验证和授权有些困惑。由于我倾向于不真正使用任何脚手架的东西,所以我首先要使用数据库,所以不希望asp.net身份为我创建表。我对散列和加盐密码很酷,并对照数据库检查用户,我遇到的麻烦是将用户设置为登录状态并检查他们应该能够访问什么。我真的很想…
ASP.NET MVC ExecuteResult与ActionResult - c#我见过它用来向响应添加标头,然后返回文件进行流传输。public override void ExecuteResult(ControllerContext context) { ... response.AddHeader("Accept-Ranges", "bytes"); response.AddHeader(&…
ASP.NET MVC Core 3.0 API将枚举序列化为字符串 - c#如何在ASP.NET MVC Core 3.0中将Enum字段序列化为String而不是Int?我不能用旧的方式做。services.AddMvc().AddJsonOptions(opts => { opts.JsonSerializerOptions.Converters.Add(new StringEnumConverter()); }) 我收到…
ddl在服务器中未更新-asp.net - javascript我在ASP.NET c#上工作。我有一个DropDownList。 (runat =“ server”)在$ {document).ready上,我更新了它的值:$(document).ready(function () { document.getElementById("ddl").value = "abc"; ……
弹出表单中的ajax数据发布未命中控制器方法ASP.NET MVC - c#我有一个弹出表单AddorEdit.cshtml,从index.cshtml页面调用。表单正在打开,但无法将数据发布到控制器方法。索引页// index.cshtml // <a class="btn btn-success" style="margin-bottom:10px;" onclick="P…