0

我有一个操作方法 VerifyNewUser(),当用户单击其电子邮件中的 url(注册验证 url)时会调用该方法。

action 方法在 ViewBag 中设置一个 bool 属性,将用户更新为经过验证的用户,然后我加载主页,用户自动登录。在 /Home/Index 视图中,我想检查 ViewBag 中我设置的属性如果为真,则显示一个 jquery ui 对话框。

但是,我的 ViewBag 为 null 并且脚本被跳过。注意我将消息存储在 homeController.ViewBag 中,所以我认为这会起作用。也许没有 ViewBag 有更好的方法来做到这一点?

public ActionResult VerifyNewUser()
    {
        if(everything checks out)  
        {
              HomeController homeController = new HomeController();
              homeController.ViewBag.RegisterationLoad = true;
              homeController.ViewBag.VerificationMessage = "Thank you! Your account has been activated";
              return View("../Home/Index", null);
        }
    }

家庭控制器没什么特别的:

    public ActionResult Index(){ 
        return View();
    }

在主页视图中,我有这段代码应该在单击验证 url 后检查主页是否正在加载,并且应该显示一个 jquery ui 对话框:

   @if (ViewBag.RegistrationLoad == "true")
   {
<script type="text/javascript">
    $("<div></div>").html("<span>@ViewBag.VerificationMessage</span>").dialog({
        width: 365, height: 165, minWidth: 365, minHeight: 165, maxWidth: 365, maxHeight: 165,
        autoOpen: true, modal: true, dialogClass: 'noTitleDialog', position: "center",
        buttons: {
            "Ok": function () {
                $(this).remove();
            }
        }
    });
</script>   
   }

谢谢你的时间

4

1 回答 1

0

不要使用任何控制器来设置里面的数据属性ViewBag。只需正常设置它,它就可以在任何视图中访问。只要确保你正确使用它。在您的代码中ViewBag,您在内部设置了一个布尔标志,并将其与一个总是会失败的字符串进行比较。

尝试这个。

public ActionResult VerifyNewUser()
{
    if(everything checks out)  
    {
          ViewBag.RegisterationLoad = true;
          ViewBag.VerificationMessage = "Thank you! Your account has been activated";
          return View("../Home/Index", null);
    }
}

在视图中

   @if (ViewBag.RegistrationLoad == true)
   { 
        .....
        .....
   }
于 2012-03-22T00:01:00.130 回答