1

i have this class in the model

public class Tenant :User
    {
        [Required]
        public string PassportNumber { get; set; }
        [Required]
        public string image { get; set; }
        [Required]
        public string passportImage { get; set; }
    }

in the view i have this code :

@using (Html.BeginForm("RegisterTenant", "Tenant", FormMethod.Post, 
                            new { enctype = "multipart/form-data" })){


    <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(x => x.FirstName, new {placeholder = "Enter Your First Name" })
            @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.LastName, new { placeholder = "Enter Your Last Name"})
            @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { placeholder = "Enter Your Password"})
            @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-field">
        <label>Password Again</label>
        <input type="text" placeholder="Enter your password again" name="Password2"/>
        <span class="errorMessage"></span>
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.MobileNumber)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.MobileNumber, new { placeholder = "Enter Your Mobile Number"})
            @Html.ValidationMessageFor(model => model.MobileNumber)
    </div>
    <div class="editor-label">
            @Html.LabelFor(model => model.PassportNumber)
    </div>
    <div class="editor-field">
            @Html.TextBoxFor(model => model.PassportNumber, new {placeholder = "Enter Your Passport Number"})
            @Html.ValidationMessageFor(model => model.PassportNumber)
    </div>
    <div class="editor-field">
        <label for="file">Upload You Passport:</label> 
        <input type="file" name="file" id="passport" style="width: 100%;" />  
        <span class="errorMessage"></span>
    </div>
    <div class="editor-field">
        <label for="file">Upload You Image:</label> 
        <input type="file" name="file" id="image" style="width: 100%;" />  
        <span class="errorMessage"></span>
    </div>
    <input type="submit" value="Register"  class="submit"/>
}

my question

even though i used validation message and the required tag, when i press the submit button, it works though the fields are empty.

what am i doing wrong?

tereško
  • 58,060
  • 25
  • 98
  • 150
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

1 Answers1

3

When you are going to use JQuery validation in a view, you have to include the required JQuery validation files in the view.

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
  • I used this `[Required(ErrorMessage= "Required")]` in my model, and yes it works, but also it works just if the field are empty not if i entered special chars or numbers ..., why please – Marco Dinatsoli Oct 27 '13 at 09:13
  • no no buddy, i want some textfields to accept just chars and others to accept just numbers (like mobile). Also, i have two password fields, i want to be sure that they are equal before sending to server, if you want i can make another questoin and you answer there. Thus, you got more requtations :P – Marco Dinatsoli Oct 27 '13 at 09:23
  • of course, you need mask facility in your view, if you put another question, I will intruce you a best one. – Abbas Amiri Oct 27 '13 at 09:26
  • please follow this question http://stackoverflow.com/questions/19616531/mvc4-many-types-of-validation – Marco Dinatsoli Oct 27 '13 at 09:30
  • please check my question, i – Marco Dinatsoli Oct 27 '13 at 09:34