Html.ValidationSummary始终显示

时间:2019-02-18 09:17:18

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

我有一个包含几个必填字段的视图-但是,当用户单击“添加”而不是页面加载时,应该触发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")
}

Site.Css

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

没有单击添加按钮的View的快照:-

View looks like this 我想念什么?

1 个答案:

答案 0 :(得分:1)

您希望此验证文本仅在发送数据后显示-因此,对于您的方案而言,仅当HTTP请求的当前方法不是“ GET”请求时才显示该验证文本:

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

这是来自经验(不要把它当作无误的事实),但是尽可能地,最好不要在不需要时渲染任何东西,而不是渲染然后隐藏

(使用CSS)(如果您重复使用代码又会怎样?类更改?为什么将验证信息公开给尚未执行POST的人?)

相关问题