【问题标题】:Data Annotations in MVC , Required is not showing on button clickMVC 中的数据注释,按钮单击时未显示必需
【发布时间】:2021-04-22 03:12:45
【问题描述】:

我正在练习 MVC Web Application .NET Framework。我在这里感到困惑,因为我已经安装了 Unobtusive.Validation 的 jQuery 库的 NuGet 包,但必填字段仍然无法正常工作。这是我的学生班

    {
        [Required (ErrorMessage ="Student Id is required")] 
        public int Id { get; set; }
        [Required(ErrorMessage = "Name is required")]
        public string  Name { get; set; }
        [Required(ErrorMessage = "Email is required")]
        public string Email { get; set; }
        [Required(ErrorMessage = "Contact is required")]
        public string Contact{ get; set; }
    }

这里是StudentData.cshtml文件,里面是c#和Html的结合代码。


@{
    ViewBag.Title = "Student Data";
}

<h2>StudentData</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
<div class="form-horizontal">
    
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Id, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save Record" class="btn btn-primary" />
        </div>
    </div>
</div>
}

我该如何解决这个问题。 我已经重新安装了 NuGet 包Unobtusive 最新版本。

【问题讨论】:

  • 什么验证不起作用?客户端还是服务器?而且我认为您必须展示如何下载 javascript 库。
  • 客户端验证不起作用,需要一个名为“Unobtusive”的 NuGet 包,我已经安装了 2 次,您也可以从 github 链接查看:github.com/nabeel-1998/WebApplication7.git
  • 您是否在浏览器中检查了错误以及是否下载了所有这些 jquery 库?
  • 不需要这样的库,只需要 NuGet 包。你也可以在 github 上查看这个项目。链接已在评论中
  • 例如,Chrome 有一个开发者工具。您必须打开它并检查错误并检查 html 代码。

标签: c# html asp.net visual-studio model-view-controller


【解决方案1】:

我以前也遇到过同样的问题。我认为程序的javascript文件相互冲突,最好检查如何引入项目的javascript文件。

【讨论】:

    【解决方案2】:

    要使客户端验证正常工作,您需要 jQuery、jQuery Validation pluginjQuery Unobtrusive Validation。您可以从 CDN 加载所有三个,例如:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.11/jquery.validate.unobtrusive.min.js"></script>

    您可以将脚本引用添加到您的视图以测试它是否有效,但通常您会将 jQuery 添加到 _Layout.cshtml 并将另外两个添加到名为 _ValidationScriptsPartial.cshtml 的局部视图中。如果您使用最新的项目模板,这是默认配置。

    在此处查看所需的配置:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多