简体   繁体   English

IIS 无法识别视图 model 注释

[英]IIS doesn't recognise view model annotations

I have a basic MVC view model with annotations, for example:我有一个带有注释的基本 MVC 视图 model,例如:

    [Required(ErrorMessage="Your Name Required")]
    [Display(Name = "Your Name")]
    [DataType(DataType.Text)]
    [MaxLength(120, ErrorMessage = "Must be under 120 characters")]                
    public String  YourName { get; set; }

I have a strongly-typed view based upon this view model.我有一个基于此视图 model 的强类型视图。 When I run the application locally, the following code generates "Your Name" label:当我在本地运行应用程序时,以下代码会生成“Your Name”label:

@Html.LabelFor(model => model.YourName)

When the application is deployed on IIS7 with .NET 4 application pool, the label says "YourName" (without a space).当应用程序部署在具有 .NET 4 应用程序池的 IIS7 上时,label 显示“YourName”(没有空格)。

This is very bizzare and I didn't come across this before.这很奇怪,我以前没有遇到过。 Does anybody have an idea what might be causing this?有人知道可能是什么原因造成的吗?

Cache is cleared, this has been tested from a range of web clients and result is the same.缓存已清除,已从一系列 web 客户端测试,结果相同。

Edit:编辑:

@model MVC.Web.Models.ContactUsModel

<div>
    @Html.LabelFor(model => model.YourName)
    @Html.EditorFor(model => model.YourName)       
</div>

Edit 2 All annotations on that field are being ignored.编辑 2该字段上的所有注释都将被忽略。 There are other text type fields and they have the same issue.还有其他文本类型字段,它们也有同样的问题。 This is happening only on a live server.这仅在实时服务器上发生。 Live server is IIS 7 which has been configured over Plesk 10.2.实时服务器是 IIS 7,已在 Plesk 10.2 上配置。 Wondering whether this is a bug since I'm using MVC 3 RTM.想知道这是否是一个错误,因为我使用的是 MVC 3 RTM。

Edit 3 Within the same view model I have the Email property:编辑 3在同一视图 model 我有 Email 属性:

    [Required(ErrorMessage = "Your Email Is Required")]
    [Display(Name = "Your Email")]
    [RegularExpression(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessage = "Your Email Is Invalid")]
    [DataType(DataType.Text)]
    public String FromEmail { get; set; }

This property is used within a view:此属性在视图中使用:

    <div>
        @Html.LabelFor(model => model.FromEmail)    
        @Html.EditorFor(model => model.FromEmail)        
    </div>

But it works perfectly fine:( So the email property works fine in both live and dev environment. Other properties work only in dev environment.但它工作得非常好:(因此 email 属性在实时和开发环境中都可以正常工作。其他属性仅在开发环境中工作。

Edit 4 Removing MaxLength and MinLength annotations fixed the problem.编辑 4删除 MaxLength 和 MinLength 注释解决了这个问题。 I would still like to use MaxLength and MinLength annotations as part of my model validation routines though.我仍然想使用 MaxLength 和 MinLength 注释作为我的 model 验证例程的一部分。

[MinLength(3, ErrorMessage = "Minimum 3 Characters")]
[MaxLength(30, ErrorMessage = "Maximum 30 Characters")]

In your example you have the following property on your Model.在您的示例中,您的 Model 上有以下属性。

[Required(ErrorMessage="Your Name Required")]
[Display(Name = "Your Name")]
[DataType(DataType.Text)]
[MaxLength(120, ErrorMessage = "Must be under 120 characters")]                
public String  YourName { get; set; }

So, the property "YourName" should get a label of "Your Name".因此,属性“YourName”应该获得“Your Name”的 label。

In your view, though, you aren't displaying the YourName property.但是,在您看来,您并没有显示 YourName 属性。

@Html.LabelFor(model => model.FromName)
@Html.EditorFor(model => model.FromName)  

So you need to add similar attributes to your FromName property.因此,您需要在 FromName 属性中添加类似的属性。

MVC3 supports the [StringLength] attribute, while [MinLenght] and [MaxLength] come with Entity Framework. MVC3 支持 [StringLength] 属性,而 [MinLenght] 和 [MaxLength] 带有实体框架。 Could you try this instead of MaxLength?你可以试试这个而不是 MaxLength 吗?

[StringLength(120, ErrorMessage = "Must be under 120 characters")]  

I'm very new to razor but I thought DisplayTextFor didn't give you a label.我对 razor 很陌生,但我认为 DisplayTextFor 没有给你 label。 Wouldn't @Html.LabelFor() make more sense? @Html.LabelFor()不会更有意义吗?

I've just tested this on IIS7 with .NET 4 App Pool, just like in your case and it works absolutely fine for me.我刚刚使用 .NET 4 应用程序池在 IIS7 上对此进行了测试,就像您的情况一样,它对我来说绝对没问题。 Can you describe how you do your deployment?你能描述一下你是如何进行部署的吗? I did a simple Right Click>Publish in Visual Studio 2010 with default profile.我使用默认配置文件在 Visual Studio 2010 中进行了简单的右键单击>发布。 Also one thing to check is that .NET version installed on the server is up-to-date.另外要检查的一件事是服务器上安装的 .NET 版本是最新的。 It should be v4.0.30319 or over.它应该是 v4.0.30319 或更高版本。 Perhaps I could email you my solution so you could test it on your server?也许我可以 email 你我的解决方案,这样你就可以在你的服务器上测试它?

Answer 2 - My first answer really just points out the mis-type in the question, but based on the comments you've given about your deployment, I think your problem might be related to your deploy, rather than your code.答案 2 - 我的第一个答案实际上只是指出了问题中的错误类型,但根据您对部署的评论,我认为您的问题可能与您的部署有关,而不是与您的代码有关。

If you are performing an x-copy deploy, make sure you are copying all the latest DLLs into the bin directory.如果您正在执行 x-copy 部署,请确保将所有最新的 DLL 复制到 bin 目录中。 What I mean is...我的意思是...

  • Make sure you build your solution, and the build succeeds确保构建解决方案,并且构建成功
  • Copy the newly built DLLs to your web server as well as the other files将新建的 DLL 复制到您的 web 服务器以及其他文件

The reason for this is that the model will get compiled into these DLLs, so you need to upload them to your live bin folder get your attributes on the server.这样做的原因是 model 将被编译到这些 DLL 中,因此您需要将它们上传到您的 live bin 文件夹中,以便在服务器上获取您的属性。

If you are uploading your model code files, they aren't actually used on the server... just the bin, views, scripts and content folders normally.如果您要上传 model 代码文件,它们实际上并没有在服务器上使用……通常只是 bin、views、scripts 和 content 文件夹。

You need the MicrosoftMvcValidation.js scripts included in your project for this to work.您需要项目中包含的 MicrosoftMvcValidation.js 脚本才能正常工作。 Did you, perhaps, remove them or set them not to copy?您是否可能删除了它们或将它们设置为不复制?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM