1

モデルにこのクラスがあります

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タグを使用しましたが、送信ボタンを押すと、フィールドが空でも機能します。

私は何を間違っていますか?

4

1 に答える 1

3

ビューで JQuery 検証を使用する場合は、必要な JQuery 検証ファイルをビューに含める必要があります。

@section scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
于 2013-10-27T09:10:11.630 に答える