Domanda

I work in ASP MVC3. I have a model for the input of the contact information of a customer. The information is required. When the viewstat is not valid the textboxes must have a red border (this works) en no errormessage. But when the emailinput is invalid the the error must be visible on the view. So only the errormessage of the emailannotation must be visible and not of the requiredannotation. I have no idea how to do this.

model:

    [Required]
    public string FirstName { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string PostalCode { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public string Country { get; set; }
    [Required]
    public string PhoneNumber { get; set; }
    public string Fax { get; set; }
    [Required]
    [Email]
    public string Email { get; set; }

view:

<div class="row-fluid">
    <div class="span6">
                <div data-role="fieldcontain" class="ui-hide-label">
                    <label for=@Lingo.language.Obj_Telefoon>@Lingo.language.Obj_Telefoon</label>
                    <span>@Html.TextBoxFor(m => m.PhoneNumber, new { style = "width:90%!important;", placeholder = @Lingo.language.Obj_Telefoon })</span>
                </div>               
    </div>
    <div class="span6">
        <div data-role="fieldcontain" class="ui-hide-label">
            <label for=@Lingo.language.Obj_Fax>@Lingo.language.Obj_Fax</label>
            <span>@Html.TextBoxFor(m => m.Fax, new { placeholder = @Lingo.language.Obj_Fax })</span>
        </div>  
    </div>
 </div>
 <div class="row-fluid">
    <div class="span12">
        <div data-role="fieldcontain" class="ui-hide-label">
            <label for=@Lingo.language.Obj_Email>@Lingo.language.Obj_Email</label>
            <span>@Html.TextBoxFor(m => m.Email, new { style = "width:98%!important;", placeholder = @Lingo.language.Obj_Email })</span>
            <span>@Html.ValidationMessageFor(m => m.Email)</span>
        </div>  
    </div>
 </div>
È stato utile?

Soluzione

If you want it to be required, but don't want to show the "required" error message, you could set the message to be a single space (to avoid the run-time error "Either ErrorMessageString or ErrorMessageResourceName must be set, but not both.")

[Required(ErrorMessage = " ")]
[Email(ErrorMessage = "Must be a valid email address.")]
public string Email { get; set; }

If you don't already have the Email attribute, you can create a class file (e.g. EmailAttribute.cs) containing:

using System.ComponentModel.DataAnnotations;

public class EmailAttribute: RegularExpressionAttribute {
    public EmailAttribute()
      : base(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$") { }
  }

Altri suggerimenti

write with required message like this:

[Required( ErrorMessage = "The first name is required" )]
public string FirstName { get; set; }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top