使验证在ASP.net MVC中可见时使标签可见 [英] Make Label visible when validation visible in ASP.net MVC

查看:36
本文介绍了使验证在ASP.net MVC中可见时使标签可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击在视图上提交"时,系统将重定向到新视图.如果验证失败,系统将重定向到同一页面,突出显示字段.但是,我还要求系统显示带有错误消息的标签.

When I click submit on view, the system redirects to a new view. If validation fails, the system redirects to the same page highlighting the fields. However, I also require the system to show a label with the error message.

<label for="label6" style="color:red; font-weight:normal !important; visibility: hidden">
    The Corporate Email, FEIN or SSN does not match.<br /><br />
    <span style="margin-left: 10%"></span>The following might be the case:<br />
    <span style="margin-left: 12%"></span>- The Corporate Email entered is wrong. Please check the Corporate Email.<br />
    <span style="margin-left: 12%"></span>- The FEIN entered is wrong. Please check the FEIN.<br />
    <span style="margin-left: 12%"></span>- The SSN entered is wrong. Please check the SSN.<br />
    <span style="margin-left: 12%"></span>- Please verify whether FEIN or SSN entered during registration.<br />
    <span style="margin-left: 16%"></span>If you have registered using FEIN please submit using FEIN.<br />
    <span style="margin-left: 16%"></span>If you have registered using SSN please submit using SSN.<br />
</Label>

有人可以帮我吗?

推荐答案

在控制器中

    public ActionResult Index()
    {
        if (ModelState.IsValid)
        {
            //do something
            return Redirect("/yournewpage");
        }

        if (!someChecks)
        {
            ModelState.AddModelError("", "Your custom error");
        }

        if (!someChecks2)
        {
            ModelState.AddModelError("", "Your custom error 2");
        }

        return View();
    }

您可以在视图中使用

<p>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<p>

这篇关于使验证在ASP.net MVC中可见时使标签可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆