【问题标题】:Html.ValidationSummary always showingHtml.ValidationSummary 总是显示
【发布时间】:2019-02-18 09:17:18
【问题描述】:

我有一个包含几个必填字段的视图 - 但是应该在用户单击“添加”而不是在页面加载时触发 ValidationSummary 消息。

我尝试了以下链接,但这些结果导致 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")
}

网站.css

.validation-summary-valid
{
display:none;
}

没有点击添加按钮的视图截图:-

我错过了什么?

【问题讨论】:

  • 你设置了这个“.validation-summary-valid { display:none; }” ???
  • @NamanUpadhyay - 查看编辑后的代码 - 仍然是同样的错误...
  • 好的,你的代码中有不同的类名,所以试试这个“validation-summary-valid text-danger { display:none; }”
  • @NamanUpadhyay - 尝试了你的建议,同样的结果 - 仍然不起作用
  • 将@Html.ValidationSummary("", new { @class= "validation-summary-valid text-danger" }) 放入表单标签

标签: c# asp.net-mvc razor data-annotations


【解决方案1】:

您希望此验证文本仅在数据已发送时出现 - 因此它适用于您的场景,仅在 HTTP 请求的当前方法不是“GET”请求时才显示:

@if (Request.HttpMethod != "GET") {
    Html.ValidationSummary("", new { @class = "validation-summary-valid text-danger" });
}

这个来自经验(不要把它当作绝对可靠的事实),但是当你知道你不需要时,最好不要渲染它,而不是渲染它然后隐藏它与 CSS (如果你重用代码怎么办?类更改?为什么将验证信息暴露给没有做过 POST 的人?)?

【讨论】:

    猜你喜欢
    • 2011-05-09
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 2013-07-01
    • 2015-09-12
    • 2014-10-23
    相关资源
    最近更新 更多