Html.ValidationSummary 始终显示
Html.ValidationSummary always showing
我有一个视图,其中有几个必填字段 - 但是当用户单击 "Add" 时应该触发 ValidationSummary 消息,而不是在页面加载时触发。
我在下面尝试过 link 但这些结果在 ValidationSummary 中完全消失了...
MVC Razor Validation Errors showing on page load when no data has been posted
public ActionResult Index(ParticipantViewModel p)
{
if (p.ClientName != null)
{
string[] split = p.ClientName.Split(',');
p.ClientName = split[0];
p.ClientConn = split[1];
d.Connection.ConnectionString = p.ClientConn;
var prt = d.Participants.Where(s => s.Id == p.Id).FirstOrDefault();
if (prt != null)
{
p.FirstName = prt.FirstName;
p.LastName = prt.LastName;
p.PayrollNo = prt.PayrollNo;
p.Id = prt.Id;
}
//ModelState.Clear();
return View(p);
}
else
{
return RedirectToAction("SearchParticipants");
}
}
视图中的代码:-
@Html.ValidationSummary("", new { @class = "validation-summary-valid text-danger" })
@using (Html.BeginForm("Add", "Participants", FormMethod.Post, new {@class = "form-horizontal", role =""}))
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.FirstName)</th>
<th>@Html.DisplayNameFor(model => model.LastName)</th>
<th>@Html.DisplayNameFor(model => model.PayrollNo)</th>
<th>@Html.DisplayNameFor(model => model.TransDesc)</th>
<th>@Html.DisplayNameFor(model => model.Points)</th>
<th>@Html.DisplayNameFor(model => model.Actions)</th>
</tr>
<tr>
<td>@Html.DisplayFor(model => model.FirstName)</td>
<td>@Html.DisplayFor(model => model.LastName)</td>
<td>@Html.DisplayFor(model => model.PayrollNo)</td>
<td>@Html.TextBoxFor(model => model.TransDesc)</td>
<td>@Html.TextBoxFor(m => m.Points, new { onkeydown = "return ValidateNumber(event);"})</td>
<td>@Html.EnumDropDownListFor(model => model.Actions)</td>
</tr>
</table>
if (Model.FirstName != null)
{
<div class="form-actions no-color">
<input type="submit" value="Add" class="btn btn-primary"/>
</div>
}
}
@section Scripts {
<script type="text/javascript" language="javascript">
function ValidateNumber(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 96 || charCode > 105))
{
return false;
}
return true;
};
</script>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
}
Site.Css
.validation-summary-valid
{
display:none;
}
没有点击添加按钮的视图截图:-
我错过了什么?
您希望此验证文本仅在发送数据时出现 - 因此仅当 HTTP 请求的当前方法不是 "GET" 请求时才显示它对您的场景有效:
@if (Request.HttpMethod != "GET") {
Html.ValidationSummary("", new { @class = "validation-summary-valid text-danger" });
}
这是经验之谈(不要把它当成绝对可靠的事实),但是当你知道你不需要渲染某些东西时,最好不要渲染它,而不是渲染它并且然后用 CSS 隐藏它(如果你重用代码怎么办?class 更改?为什么要向没有做过 POST 的人公开验证信息?)
我有一个视图,其中有几个必填字段 - 但是当用户单击 "Add" 时应该触发 ValidationSummary 消息,而不是在页面加载时触发。
我在下面尝试过 link 但这些结果在 ValidationSummary 中完全消失了...
MVC Razor Validation Errors showing on page load when no data has been posted
public ActionResult Index(ParticipantViewModel p)
{
if (p.ClientName != null)
{
string[] split = p.ClientName.Split(',');
p.ClientName = split[0];
p.ClientConn = split[1];
d.Connection.ConnectionString = p.ClientConn;
var prt = d.Participants.Where(s => s.Id == p.Id).FirstOrDefault();
if (prt != null)
{
p.FirstName = prt.FirstName;
p.LastName = prt.LastName;
p.PayrollNo = prt.PayrollNo;
p.Id = prt.Id;
}
//ModelState.Clear();
return View(p);
}
else
{
return RedirectToAction("SearchParticipants");
}
}
视图中的代码:-
@Html.ValidationSummary("", new { @class = "validation-summary-valid text-danger" })
@using (Html.BeginForm("Add", "Participants", FormMethod.Post, new {@class = "form-horizontal", role =""}))
{
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.FirstName)</th>
<th>@Html.DisplayNameFor(model => model.LastName)</th>
<th>@Html.DisplayNameFor(model => model.PayrollNo)</th>
<th>@Html.DisplayNameFor(model => model.TransDesc)</th>
<th>@Html.DisplayNameFor(model => model.Points)</th>
<th>@Html.DisplayNameFor(model => model.Actions)</th>
</tr>
<tr>
<td>@Html.DisplayFor(model => model.FirstName)</td>
<td>@Html.DisplayFor(model => model.LastName)</td>
<td>@Html.DisplayFor(model => model.PayrollNo)</td>
<td>@Html.TextBoxFor(model => model.TransDesc)</td>
<td>@Html.TextBoxFor(m => m.Points, new { onkeydown = "return ValidateNumber(event);"})</td>
<td>@Html.EnumDropDownListFor(model => model.Actions)</td>
</tr>
</table>
if (Model.FirstName != null)
{
<div class="form-actions no-color">
<input type="submit" value="Add" class="btn btn-primary"/>
</div>
}
}
@section Scripts {
<script type="text/javascript" language="javascript">
function ValidateNumber(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 96 || charCode > 105))
{
return false;
}
return true;
};
</script>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
}
Site.Css
.validation-summary-valid
{
display:none;
}
没有点击添加按钮的视图截图:-
您希望此验证文本仅在发送数据时出现 - 因此仅当 HTTP 请求的当前方法不是 "GET" 请求时才显示它对您的场景有效:
@if (Request.HttpMethod != "GET") {
Html.ValidationSummary("", new { @class = "validation-summary-valid text-danger" });
}
这是经验之谈(不要把它当成绝对可靠的事实),但是当你知道你不需要渲染某些东西时,最好不要渲染它,而不是渲染它并且然后用 CSS 隐藏它(如果你重用代码怎么办?class 更改?为什么要向没有做过 POST 的人公开验证信息?)