I have a ViewBag string message. This message is passed from an action link into a control method. Some logic can then be used within the action method based on the viewbag message.
Likewise the action result can set the view bag message, so that when a view page is loaded, the jquery compares the viewbag message and if there are any matches, will display a toast message.
This requires that I have control over the flow of the program. So I know which method is going to handle the actionlink.
The problem is, using the entity framework's user identity. If the method is blocked by authorization, the method is automatically bypassed and the user is presented with the login page.
This means, my razor is not passing any information to that particular method.
ActionLink has viewbag message -> passes as parameter to controller
Controller sets message -> when page is loaded, message is checked.
I have been passing a string ViewBag.Message from views to controllers, using toastr to create pop ups throughout my project. It works well.
I have a project that uses [Authorize] from the inbuilt UserIdentity when a user wants to check out.
[Authorize]
public ActionResult Checkout(string message)
{
The problem is, the message is never passed to the LoginAction method.
[AllowAnonymous]
public ActionResult Login(string message,string returnUrl)
{
I have tried creating a message within the view.
@{
string message = ViewBag.Message;
}
/.../
@Html.ActionLink("Go To Check Out", "Checkout", "Carts",
new {message = "You Need to Login to Checkout!" })
I have tried working out if I can determine which link was used to call the Login view. I've been working on it on and off for a while.
I cannot figure out how to do this and I think it would be good if I knew how.
Any help is appreciated.
Edit
How it works:
[HttpPost]
public ActionResult RemoveFromCart(int prod_id)
{
Cart cart = (Cart)Session["Cart"];
cart.RemoveFromCart(prod_id);
return RedirectToAction("Index", new { message = "Cart Updated!" });
}
With the index:
@section Scripts {
<script src="~/Scripts/jquery-2.1.4.js"></script>
<link href="~/Content/toastr.css" rel="stylesheet" />
<script src="~/Scripts/toastr.js"></script>
<script src="~/Scripts/toastr.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
if ('@ViewBag.Message' == "Cart Updated!") {
toastr.options.timeOut = 1500; // 1.5s
toastr.success('@ViewBag.Message');
}
if ('@ViewBag.Message' == "Login Successful!") {
toastr.options.timeOut = 1500; // 1.5s
toastr.success('@ViewBag.Message');
}
if ('@ViewBag.Message' == "Registration Successful!") {
toastr.options.timeOut = 1500; // 1.5s
toastr.success('@ViewBag.Message');
}
});
</script>
}
So in actual fact the viewbag message is set in the controller and then the message compared (in the jquery) when the page is loaded.
So I am trying to do it back to front, because I'm at my wits end in working out how to do it.
So I want to do a check int the Account/login controller I would need to check to see if the login came from a user attempting to check out without logging in, and as it just redirects there using the [Authorize] security checks, I don't know how to do this.
[AllowAnonymous]
public ActionResult Login(string message,string returnUrl)
{
if (message == null)
{
message = "";
}
// Algorithm - IF this was redirected from an attempt to checkout without being logged in. Have the view bag message =="You Need to Login to Checkout!"
ViewBag.ReturnUrl = returnUrl;
return View();
}
This way when the Login page loads, there will be a toast message, as the message is checked when each page is loaded.
edit 402,128,841,442,222 and still not wiki ;)
The problem is explained by the following.
Similarities between ViewBag & ViewData:
Helps to maintain data when you move from controller to view.
Used to pass data from controller to corresponding view.
Short life means value becomes null when redirection occurs.
This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.
So I guess I need to find another way of doing this, any ideas welcome.