【发布时间】: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