I have an action method VerifyNewUser() that gets called when the user clicks a url in their email (registration verification url).
The action method sets a bool property in the ViewBag, updates the user to a verified user, then I load the home page with the user automatically signed in. In the /Home/Index view I want to check the ViewBag for the property I set and display a jquery ui dialog if its true.
However, my ViewBag is null and the script gets skipped. Note I store the message in the homeController.ViewBag so I thought this would work. Perhaps theres a better way to do it this without a 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);
}
}
nothing special in Home controller:
public ActionResult Index(){
return View();
}
In the home view i have this code thats supposed to checks if the homepage is being loaded after clicking the verification url and should display a jquery ui dialog:
@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>
}
Thanks for your time
Don’t use any controller to set the data attributes inside
ViewBag. Just set it normally and it can be accessible inside any view. Just make sure you use it correctly. In your code you are setting a boolean flag insideViewBagand you are comparing it against a string which will always fail.Try this.
In the view