[asp.net-mvc]验证不是工作 mvc

标签: Asp.net-MVC HTML
发布时间: 2016/10/9 6:15:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我只是找不到出了什么错误。我想不允许设置空值字段,当用户试图这样做,它必须显示消息"*",等待他的设置值,但它不工作和空值成功发送到行动。 型号︰

public class CompanyMainInfoModel
{
    public int CompanyId { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_name", ResourceType = typeof(Localization))]
    public string CompanyName { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_address", ResourceType = typeof(Localization))]
    public string CompanyAddress { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_director", ResourceType = typeof(Localization))]
    public string CompanyDirector { get; set; }

    [Required(ErrorMessage = "*")]
    [Display(Name = "company_phone", ResourceType = typeof(Localization))]
    public string CompanyTelephoneNumber { get; set; }
}

标记︰

    @model BTGHRM.Models.CompanyMainInfoModel
@{
    Layout = "~/Views/Shared/_EmployeeMain.cshtml";
}

<head>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/jquery-ui.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="~/Scripts/jquery.validate.js"></script>
</head>

<body>

    <span class="content_h4">@Resources.Localization.company_info</span>
    <br />
    <br />

    <div id="FormContainer">
        @Html.Partial("Partial/_CompanyInfo", Model)
    </div>


</body>

部分标记︰

    @model BTGHRM.Models.CompanyMainInfoModel

@using (Html.BeginForm("CompanyInfo", "Administration"))
{
    <table>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyName)</td>
            <td>@Html.TextBoxFor(m => m.CompanyName)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyName, "" , new { @class="text-danger"})</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyAddress)</td>
            <td>@Html.TextBoxFor(m => m.CompanyAddress)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyAddress, "", new { @class = "text-danger" })</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyDirector)</td>
            <td>@Html.TextBoxFor(m => m.CompanyDirector)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyDirector, "", new { @class = "text-danger" })</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.CompanyTelephoneNumber)</td>
            <td>@Html.TextBoxFor(m => m.CompanyTelephoneNumber)</td>
            <td>@Html.ValidationMessageFor(m => m.CompanyTelephoneNumber, "", new { @class = "text-danger" })</td>
        </tr>
    </table>
    <input style="width:78px" type="submit" value=@Resources.Localization.save />
}

并且尽管 [要求] 它仍然让我到任何字段设置为 null 值。 我的代码有什么错误?

解决方法 1:

你必须有不显眼的脚本在 web 配置中启用

<add key="UnobtrusiveJavaScriptEnabled" value="true"/>

然后,您需要添加

  • jquery
  • jquery.validate.js
  • jquery.validate.unobtrusive.js
官方微信
官方QQ群
31647020