簡體   English   中英

@Html.ValidationMessageFor 不適用於表

[英]@Html.ValidationMessageFor is not working with table

我的 MVC5 應用程序中有以下視圖,但問題是 Html.ValidationMessageFor 助手不起作用。 文本框旁邊沒有顯示錯誤消息。

<div class="container" style="max-width:650px;padding:40px 20px;background:#1F618D;opacity: 0.9;margin-top:30px;color:white;margin-bottom:50px;">
    <h3 style="font-family: 'Open Sans' , sans-serif;font-size: 30px;font-weight: 600;color: #ffffff;margin:5px;text-align: center;text-transform: uppercase;opacity:0.9;">Ride Details and Change Status</h3>

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <table class='table borderless'>

        <tr>
            <td>Location</td>
            <td>  @Html.TextBoxFor(m => m.Location.LocationId, new {  id = "Location", @class = "form-control", style = "max-width:280px;background-color:#98AFC7;color:#ffffff;" })</td>
        </tr>

        <tr>
            <td>Customer</td>
            <td>@Html.TextBoxFor(m => m.Customer.Username, new { id = "CustomerUsername", @class = "form-control", style = "max-width:280px;background-color:#98AFC7;color:#ffffff;" })</td>
        </tr>
        <tr>
            <td>Destination</td>
            <td>@Html.TextBoxFor(m => m.Destination.LocationId, new { id = "destination", @class = "form-control", style = "max-width:280px;" })</td>

        </tr>

        <tr>
            <td>Amount</td>
            <td>
                @Html.TextBoxFor(m => m.Amount, new { id = "amount", @class = "form-control", style = "max-width:280px;" })
                @Html.ValidationMessageFor(m => m.Amount, "", new { @class = "text-danger" })
            </td>


        </tr>


        <tr>
            <td>Change Status</td>
            <td><button type="button" id="changeStatusSuccessful" class="btn btn-secondary btn-block" style="background-color: #10354E ;color: white;width:280px">Successful</button> </td>
        </tr>
    </table>
</div>

這是我的視圖模型

    [Required]
    public double Amount { get; set; }        
    public Customer Customer { get; set; }
    public Location Location { get; set; }

    public Location Destination { get; set; }

    public Driver Driver { get; set; }
    public Comment Comment { get; set; }

您是否在視圖中包括了jQuery腳本validate.jsvalidate.unobtrusive.min.js 將此添加到視圖

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

我猜這是問題所在,但如果不起作用,請提供從視圖生成的代碼,以便我進一步提供幫助。

使用@section Scripts { @Scripts.Render("~/bundles/jqueryval") } enter code here

並在任何標簽上的 beginform @Html.ValidationSummary(true, "", new { @class = "text-danger" })

並在標簽末尾 @Html.ValidationMessageFor(m => m.{item-name}, "", new { @class = "text-danger" })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM