即使我使用了必需的MVC4验证也无法正常工作

时间:2013-10-27 08:49:16

标签: asp.net-mvc asp.net-mvc-4 html-helper

我在模型

中有这个课程
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标签,当我按下提交按钮时,虽然字段为空,但它仍然有用。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

当您要在视图中使用JQuery验证时,您必须在视图中包含所需的JQuery验证文件。

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