简体   繁体   English

验证摘要应显示在警告消息框中?

[英]validation summary should show in alert message box?

I have a view which contains a form. 我有一个包含表格的视图。 The controls have annotations in my model. 控件在我的模型中有注释。 I want that 我要那个

    @Html.ValidationSummary();

will show in a alert Message Box. 将显示在警告消息框中。

Is there any way. 有什么办法吗? I am new to mvc therefore please elaborate the answer. 我是mvc的新手,请详细说明答案。

Thanks in advance!! 提前致谢!!

I don't have my computer with me so I can't provide an example but @Html.ValidationSummary() return a unorderd list of errors. 我没有我的电脑,所以我无法提供示例,但@Html.ValidationSummary()返回一个无序的错误列表。 (see http://msdn.microsoft.com/en-us/library/dd460343(v=vs.108).aspx ) (见http://msdn.microsoft.com/en-us/library/dd460343(v=vs.108).aspx

You could check if @Html.ValidationSummary() has a value in JavaScript and then use JQuery to pull out the text values of the <li> elements and put them in an alert. 您可以检查@Html.ValidationSummary()是否在JavaScript中具有值,然后使用JQuery提取<li>元素的文本值并将它们置于警报中。

Try this, I haven't tested it yet but it should be a guide: 试试这个,我还没有测试过,但它应该是一个指南:

<script>
var validationSummary = '@Html.ValidationSummary()';
var alerttext = '';
if (validationSummary !== '')
{
    $(validationSummary)
        .find('li').each( function()
        {
            alerttext = alerttext + $(this).text() + '\n';
        });

    alert(alerttext);

}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM