即使我使用了必需的 MVC4 验证也不起作用



>我在模型中有这个类

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

视图中,我有以下代码:

@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"/>
}

我的问题

即使我使用了验证消息和required标签,当我按下提交按钮时,尽管字段为空,但它仍然可以工作。

我做错了什么?

如果要在视图中使用 JQuery 验证

,必须在视图中包含所需的 JQuery 验证文件。

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

最新更新