简体   繁体   中英

IIS doesn't recognise view model annotations

I have a basic MVC view model with annotations, for example:

    [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. When I run the application locally, the following code generates "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).

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.

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. 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. Wondering whether this is a bug since I'm using MVC 3 RTM.

Edit 3 Within the same view model I have the Email property:

    [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.

Edit 4 Removing MaxLength and MinLength annotations fixed the problem. I would still like to use MaxLength and MinLength annotations as part of my model validation routines though.

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

In your example you have the following property on your 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".

In your view, though, you aren't displaying the YourName property.

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

So you need to add similar attributes to your FromName property.

MVC3 supports the [StringLength] attribute, while [MinLenght] and [MaxLength] come with Entity Framework. Could you try this instead of 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. Wouldn't @Html.LabelFor() make more sense?

I've just tested this on IIS7 with .NET 4 App Pool, just like in your case and it works absolutely fine for me. Can you describe how you do your deployment? I did a simple Right Click>Publish in Visual Studio 2010 with default profile. Also one thing to check is that .NET version installed on the server is up-to-date. It should be v4.0.30319 or over. Perhaps I could email you my solution so you could test it on your server?

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.

If you are performing an x-copy deploy, make sure you are copying all the latest DLLs into the bin directory. 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

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.

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.

You need the MicrosoftMvcValidation.js scripts included in your project for this to work. Did you, perhaps, remove them or set them not to copy?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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