C# @Html.ValidationMessageFor无法使用表

C# @Html.ValidationMessageFor无法使用表,c#,asp.net,C#,Asp.net,我在MVC5应用程序中有以下视图,但问题是Html.ValidationMessageFor helper不起作用。文本框旁边不显示任何错误消息 <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-fa

我在MVC5应用程序中有以下视图,但问题是Html.ValidationMessageFor helper不起作用。文本框旁边不显示任何错误消息

<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>

您是否在视图中包含jQuery脚本validate.js和validate.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>


我猜这就是问题所在,但如果它不起作用,请提供从视图生成的代码,以便我进一步提供帮助。

您是否在视图中包含jQuery脚本validate.js和validate.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>


我猜这就是问题所在,但如果它不起作用,请提供从视图生成的代码,以便我进一步提供帮助。

第二个参数应该是验证消息。在你的情况下,它是空的。如果要在视图模型中设置错误消息,则只需要
@Html.ValidationMessageFor(m=>m.Amount)。请参见,在您的模型中,您需要添加
ErrorMessage
属性。您可以将其与
Required
属性一起使用:
[Required(ErrorMessage=“Amount is Required”)]
。您还可以在视图中定义验证消息,如下所示:
@Html.ValidationMessageFor(x=>x.Amount,“Amount is required”)
。我个人更喜欢用验证逻辑装饰我的模型属性。例如,我可以向字符串变量添加验证逻辑,如:
[StringLength(20,ErrorMessage=“此字符串不能超过20个字符”)]
。第二个参数应该是验证消息。在你的情况下,它是空的。如果要在视图模型中设置错误消息,则只需要
@Html.ValidationMessageFor(m=>m.Amount)。请参见,在您的模型中,您需要添加
ErrorMessage
属性。您可以将其与
Required
属性一起使用:
[Required(ErrorMessage=“Amount is Required”)]
。您还可以在视图中定义验证消息,如下所示:
@Html.ValidationMessageFor(x=>x.Amount,“Amount is required”)
。我个人更喜欢用验证逻辑装饰我的模型属性。例如,我可以向字符串变量添加验证逻辑,如:
[StringLength(20,ErrorMessage=“此字符串不能超过20个字符”)]